Sample code for 30+ languages & platforms
Visual FoxPro

Create a JWS

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.CreateJws, which creates and returns a JWS from the current payload, headers, and signing key.

Background. JWS (JSON Web Signature) integrity-protects a payload with a signature or MAC. The header alg names the algorithm; this example uses HMAC-SHA256 (HS256) with a symmetric MAC key.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loJws
LOCAL loHeader
LOCAL lnBIncludeBom
LOCAL lcBase64Key
LOCAL lcJwsCompact

lnSuccess = 0

loJws = CreateObject('Chilkat.Jws')

*  Set the protected header for signer 0, specifying the signature algorithm (alg).
loHeader = CreateObject('Chilkat.JsonObject')
loHeader.UpdateString("alg","HS256")
lnSuccess = loJws.SetProtectedHeader(0,loHeader)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    CANCEL
ENDIF

*  Set the payload to be signed.
lnBIncludeBom = 0
lnSuccess = loJws.SetPayload("This is the content to sign.","utf-8",lnBIncludeBom)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    CANCEL
ENDIF

*  Provide the HMAC key (base64) for signer 0.  In production, obtain the key from a secure source
*  rather than hard-coding it.
lcBase64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
lnSuccess = loJws.SetMacKey(0,lcBase64Key,"base64")
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    CANCEL
ENDIF

*  Create the JWS from the current payload, headers, and signing key.  The returned string is the
*  JWS (compact serialization by default).
lcJwsCompact = loJws.CreateJws()
IF (loJws.LastMethodSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    CANCEL
ENDIF

? lcJwsCompact

RELEASE loJws
RELEASE loHeader