Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJws
LOCAL lcJwsCompact
LOCAL loPubKey
LOCAL v

lnSuccess = 0

loJws = CreateObject('Chilkat.Jws')

*  Load the JWS compact serialization.
lcJwsCompact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
lnSuccess = loJws.LoadJws(lcJwsCompact)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    CANCEL
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.
loPubKey = CreateObject('Chilkat.PublicKey')
lnSuccess = loPubKey.LoadFromFile("qa_data/signer_pubkey.pem")
IF (lnSuccess = 0) THEN
    ? loPubKey.LastErrorText
    RELEASE loJws
    RELEASE loPubKey
    CANCEL
ENDIF

lnSuccess = loJws.SetPublicKey(0,loPubKey)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loPubKey
    CANCEL
ENDIF

*  Validate the signature.  Validate returns 1 when valid, 0 when not, or a negative value on error.
v = loJws.Validate(0)
IF (v < 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loPubKey
    CANCEL
ENDIF

IF (v <> 1) THEN
    ? "The signature is not valid."
    RELEASE loJws
    RELEASE loPubKey
    CANCEL
ENDIF

? "The signature is valid."

RELEASE loJws
RELEASE loPubKey