Sample code for 30+ languages & platforms
Xbase++

Load and Validate a JWS from a StringBuilder

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.LoadJwsSb, which loads a JWS from a StringBuilder (here read from a file). The example then supplies the MAC key and validates the signature.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Validate returns 1 when the signature or MAC is cryptographically valid, 0 when it is not, or a negative value on error.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJws
LOCAL oSbJws
LOCAL nBLoaded
LOCAL cBase64Key
LOCAL v

nSuccess := 0

oJws := CreateObject("Chilkat.Jws")

//  The file path is relative to the application's current working directory.  An absolute path may
//  also be used.  Supply the path appropriate to your own environment.

//  Load the JWS text from a file into a StringBuilder.
oSbJws := CreateObject("Chilkat.StringBuilder")
nBLoaded := oSbJws:LoadFile("qa_data/message.jws", "utf-8")
IF (nBLoaded != 1)
    ? "Failed to load the JWS file."
    oJws:destroy()
    oSbJws:destroy()
    RETURN
ENDIF

//  Load the JWS from the StringBuilder.
nSuccess := oJws:LoadJwsSb(oSbJws)
IF (nSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    oSbJws:destroy()
    RETURN
ENDIF

cBase64Key := "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
nSuccess := oJws:SetMacKey(0, cBase64Key, "base64")
IF (nSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    oSbJws:destroy()
    RETURN
ENDIF

v := oJws:Validate(0)
IF (v < 0)
    ? oJws:LastErrorText
    oJws:destroy()
    oSbJws:destroy()
    RETURN
ENDIF

IF (v != 1)
    ? "The signature is not valid."
    oJws:destroy()
    oSbJws:destroy()
    RETURN
ENDIF

? "The signature is valid."

oJws:destroy()
oSbJws:destroy()