Sample code for 30+ languages & platforms
Visual FoxPro

Create a JWS into a StringBuilder

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.CreateJwsSb, which creates a JWS and writes it into a StringBuilder.

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 loSbJws

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 and write it into a StringBuilder.
loSbJws = CreateObject('Chilkat.StringBuilder')
lnSuccess = loJws.CreateJwsSb(loSbJws)
IF (lnSuccess = 0) THEN
    ? loJws.LastErrorText
    RELEASE loJws
    RELEASE loHeader
    RELEASE loSbJws
    CANCEL
ENDIF

? loSbJws.GetAsString()

RELEASE loJws
RELEASE loHeader
RELEASE loSbJws