Sample code for 30+ languages & platforms
PowerBuilder

Set the Optional JWS Unprotected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.

Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jws
oleobject loo_Header
oleobject loo_Unprotected
integer li_BIncludeBom
string ls_Base64Key
string ls_JwsJson

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

//  Set the optional unprotected header for signature 0.  Unlike the protected header, it is not
//  covered by the signature.  It is carried in the JSON serialization forms of a JWS.
loo_Unprotected = create oleobject
li_rc = loo_Unprotected.ConnectToNewObject("Chilkat.JsonObject")

loo_Unprotected.UpdateString("kid","signer-key-1")
li_Success = loo_Jws.SetUnprotectedHeader(0,loo_Unprotected)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_Unprotected
    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
    destroy loo_Unprotected
    return
end if

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
    destroy loo_Unprotected
    return
end if

//  Because an unprotected header is present, the JWS is produced in a JSON serialization form.
ls_JwsJson = loo_Jws.CreateJws()
if loo_Jws.LastMethodSuccess = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_Unprotected
    return
end if

Write-Debug ls_JwsJson


destroy loo_Jws
destroy loo_Header
destroy loo_Unprotected