Sample code for 30+ languages & platforms
PowerBuilder

Sign a JWS with an HMAC Key

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetMacKey, which sets the symmetric MAC key for a signature. The key bytes are supplied in a named encoding such as hex or base64.

Background. HMAC-based JWS (for example HS256) authenticates the payload with a shared symmetric key that both signer and verifier possess.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jws
oleobject loo_Header
integer li_BIncludeBom
string ls_Base64Key
string ls_JwsCompact

li_Success = 0

loo_Jws = create oleobject
li_rc = loo_Jws.ConnectToNewObject("Chilkat.Jws")
if li_rc < 0 then
    destroy loo_Jws
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Protected header specifying HMAC-SHA256.
loo_Header = create oleobject
li_rc = loo_Header.ConnectToNewObject("Chilkat.JsonObject")

loo_Header.UpdateString("alg","HS256")
li_Success = loo_Jws.SetProtectedHeader(0,loo_Header)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    return
end if

li_BIncludeBom = 0
li_Success = loo_Jws.SetPayload("This is the content to sign.","utf-8",li_BIncludeBom)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    return
end if

//  Set the symmetric MAC key for signature 0.  The key bytes are provided in the named encoding (here
//  base64).  In production, obtain the key from a secure source rather than hard-coding it.
ls_Base64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
li_Success = loo_Jws.SetMacKey(0,ls_Base64Key,"base64")
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    return
end if

ls_JwsCompact = loo_Jws.CreateJws()
if loo_Jws.LastMethodSuccess = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    return
end if

Write-Debug ls_JwsCompact


destroy loo_Jws
destroy loo_Header