Sample code for 30+ languages & platforms
PowerBuilder

Create a JWS

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.CreateJws, which creates and returns a JWS from the current payload, headers, and signing key.

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

//  Set the protected header for signer 0, specifying the signature algorithm (alg).
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

//  Set the payload to be signed.
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

//  Provide the HMAC key (base64) for signer 0.  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

//  Create the JWS from the current payload, headers, and signing key.  The returned string is the
//  JWS (compact serialization by default).
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