Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

loJws = createobject("CkJws")

//  Set the protected header for signer 0, specifying the signature algorithm (alg).
loHeader = createobject("CkJsonObject")
loHeader.UpdateString("alg","HS256")
llSuccess = loJws.SetProtectedHeader(0,loHeader)
if (llSuccess = .F.) then
    ? loJws.LastErrorText
    release loJws
    release loHeader
    return
endif

//  Set the payload to be signed.
llBIncludeBom = .F.
llSuccess = loJws.SetPayload("This is the content to sign.","utf-8",llBIncludeBom)
if (llSuccess = .F.) then
    ? loJws.LastErrorText
    release loJws
    release loHeader
    return
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="
llSuccess = loJws.SetMacKey(0,lcBase64Key,"base64")
if (llSuccess = .F.) then
    ? loJws.LastErrorText
    release loJws
    release loHeader
    return
endif

//  Create the JWS and write it into a StringBuilder.
loSbJws = createobject("CkStringBuilder")
llSuccess = loJws.CreateJwsSb(loSbJws)
if (llSuccess = .F.) then
    ? loJws.LastErrorText
    release loJws
    release loHeader
    release loSbJws
    return
endif

? loSbJws.GetAsString()


release loJws
release loHeader
release loSbJws