Xbase++
Xbase++
Validate a JWS with a Public Key
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetPublicKey, which sets the public key used to validate a signature of a loaded 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.
Background. The public key verifies a signature created with the corresponding private key. Validate returns 1 when valid, 0 when not, or a negative value on error.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJws
LOCAL cJwsCompact
LOCAL oPubKey
LOCAL v
nSuccess := 0
oJws := CreateObject("Chilkat.Jws")
// Load the JWS compact serialization.
cJwsCompact := "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
nSuccess := oJws:LoadJws(cJwsCompact)
IF (nSuccess == 0)
? oJws:LastErrorText
oJws:destroy()
RETURN
ENDIF
// 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 signer's public key and set it for validating signature 0.
oPubKey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubKey:LoadFromFile("qa_data/signer_pubkey.pem")
IF (nSuccess == 0)
? oPubKey:LastErrorText
oJws:destroy()
oPubKey:destroy()
RETURN
ENDIF
nSuccess := oJws:SetPublicKey(0, oPubKey)
IF (nSuccess == 0)
? oJws:LastErrorText
oJws:destroy()
oPubKey:destroy()
RETURN
ENDIF
// Validate the signature. Validate returns 1 when valid, 0 when not, or a negative value on error.
v := oJws:Validate(0)
IF (v < 0)
? oJws:LastErrorText
oJws:destroy()
oPubKey:destroy()
RETURN
ENDIF
IF (v != 1)
? "The signature is not valid."
oJws:destroy()
oPubKey:destroy()
RETURN
ENDIF
? "The signature is valid."
oJws:destroy()
oPubKey:destroy()