Sample code for 30+ languages & platforms
Xbase++

Sign a JWS with an HMAC Key from BinData

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.

Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJws
LOCAL oHeader
LOCAL nBIncludeBom
LOCAL oBdKey
LOCAL cJwsCompact

nSuccess := 0

oJws := CreateObject("Chilkat.Jws")

//  Protected header specifying HMAC-SHA256.
oHeader := CreateObject("Chilkat.JsonObject")
oHeader:UpdateString("alg", "HS256")
nSuccess := oJws:SetProtectedHeader(0, oHeader)
IF (nSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    oHeader:destroy()
    RETURN
ENDIF

nBIncludeBom := 0
nSuccess := oJws:SetPayload("This is the content to sign.", "utf-8", nBIncludeBom)
IF (nSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    oHeader:destroy()
    RETURN
ENDIF

//  Set the symmetric MAC key for signature 0 from the raw bytes in a BinData.  In production, obtain
//  the key material from a secure source.
oBdKey := CreateObject("Chilkat.BinData")
oBdKey:AppendEncoded("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=", "base64")
nSuccess := oJws:SetMacKeyBd(0, oBdKey)
IF (nSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    oHeader:destroy()
    oBdKey:destroy()
    RETURN
ENDIF

cJwsCompact := oJws:CreateJws()
IF (oJws:LastMethodSuccess == 0)
    ? oJws:LastErrorText
    oJws:destroy()
    oHeader:destroy()
    oBdKey:destroy()
    RETURN
ENDIF

? cJwsCompact

oJws:destroy()
oHeader:destroy()
oBdKey:destroy()