Sample code for 30+ languages & platforms
PowerBuilder

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

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

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 and write it into a StringBuilder.
loo_SbJws = create oleobject
li_rc = loo_SbJws.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Jws.CreateJwsSb(loo_SbJws)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_SbJws
    return
end if

Write-Debug loo_SbJws.GetAsString()


destroy loo_Jws
destroy loo_Header
destroy loo_SbJws