Xbase++ Requires Chilkat v11.0.0+
Xbase++
JWS Using HMAC SHA-256
See more JSON Web Signatures (JWS) Examples
Creates a JSON Web Signatures (JWS) using HMAC SHA-256.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oJwsProtHdr
LOCAL oJws
LOCAL cHmacKey
LOCAL nSignatureIndex
LOCAL nBIncludeBom
LOCAL cPayloadStr
LOCAL cJwsCompact
LOCAL oJws2
LOCAL v
LOCAL oJoseHeader
nSuccess := 0
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First create the JWS Protected Header
oJwsProtHdr := CreateObject("Chilkat.JsonObject")
oJwsProtHdr:AppendString("typ", "JWT")
oJwsProtHdr:AppendString("alg", "HS256")
? "JWS Protected Header: " + oJwsProtHdr:Emit()
// Output:
// JWS Protected Header: {"typ":"JWT","alg":"HS256"}
oJws := CreateObject("Chilkat.Jws")
// Set the HMAC key:
cHmacKey := "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"
nSignatureIndex := 0
oJws:SetMacKey(nSignatureIndex, cHmacKey, "base64url")
// Set the protected header:
oJws:SetProtectedHeader(nSignatureIndex, oJwsProtHdr)
// Set the payload.
nBIncludeBom := 0
cPayloadStr := "In our village, folks say God crumbles up the old moon into stars."
oJws:SetPayload(cPayloadStr, "utf-8", nBIncludeBom)
// Create the JWS
// By default, the compact serialization is used.
cJwsCompact := oJws:CreateJws()
IF (oJws:LastMethodSuccess == 0)
? oJws:LastErrorText
oJwsProtHdr:destroy()
oJws:destroy()
RETURN
ENDIF
? "JWS: " + cJwsCompact
// sample output:
// JWS: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu.bsYsi8HJ0N6OqGI1hKQ9QQRNPxxA5qMpcHLtOvXatk8
// Now load the JWS, validate, and recover the original text.
oJws2 := CreateObject("Chilkat.Jws")
// Load the JWS.
nSuccess := oJws2:LoadJws(cJwsCompact)
// Set the MAC key used for validation.
nSignatureIndex := 0
oJws2:SetMacKey(nSignatureIndex, cHmacKey, "base64url")
// Validate the 1st (and only) signature at index 0..
v := oJws2:Validate(nSignatureIndex)
IF (v < 0)
// Perhaps Chilkat was not unlocked or the trial expired..
? "Method call failed for some other reason."
? oJws2:LastErrorText
oJwsProtHdr:destroy()
oJws:destroy()
oJws2:destroy()
RETURN
ENDIF
IF (v == 0)
? "Invalid signature. The MAC key was incorrect, the JWS was invalid, or both."
oJwsProtHdr:destroy()
oJws:destroy()
oJws2:destroy()
RETURN
ENDIF
// If we get here, the signature was validated..
? "Signature validated."
// Recover the original content:
? oJws2:GetPayload("utf-8")
// Examine the protected header:
oJoseHeader := CreateObject("Chilkat.JsonObject")
nSuccess := oJws2:GetProtectedH(nSignatureIndex, oJoseHeader)
IF (nSuccess == 0)
? oJws2:LastErrorText
oJwsProtHdr:destroy()
oJws:destroy()
oJws2:destroy()
oJoseHeader:destroy()
RETURN
ENDIF
oJoseHeader:EmitCompact := 0
? "Protected (JOSE) header:"
? oJoseHeader:Emit()
// Output:
// Signature validated.
// In our village, folks say God crumbles up the old moon into stars.
// Protected (JOSE) header:
// {
// "typ": "JWT",
// "alg": "HS256"
// }
oJwsProtHdr:destroy()
oJws:destroy()
oJws2:destroy()
oJoseHeader:destroy()