Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

$oJws = ObjCreate("Chilkat.Jws")

;  Protected header specifying HMAC-SHA256.
$oHeader = ObjCreate("Chilkat.JsonObject")
$oHeader.UpdateString("alg","HS256")
$bSuccess = $oJws.SetProtectedHeader(0,$oHeader)
If ($bSuccess = False) Then
    ConsoleWrite($oJws.LastErrorText & @CRLF)
    Exit
EndIf

;  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.
$oUnprotected = ObjCreate("Chilkat.JsonObject")
$oUnprotected.UpdateString("kid","signer-key-1")
$bSuccess = $oJws.SetUnprotectedHeader(0,$oUnprotected)
If ($bSuccess = False) Then
    ConsoleWrite($oJws.LastErrorText & @CRLF)
    Exit
EndIf

Local $bIncludeBom = False
$bSuccess = $oJws.SetPayload("This is the content to sign.","utf-8",$bIncludeBom)
If ($bSuccess = False) Then
    ConsoleWrite($oJws.LastErrorText & @CRLF)
    Exit
EndIf

Local $sBase64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
$bSuccess = $oJws.SetMacKey(0,$sBase64Key,"base64")
If ($bSuccess = False) Then
    ConsoleWrite($oJws.LastErrorText & @CRLF)
    Exit
EndIf

;  Because an unprotected header is present, the JWS is produced in a JSON serialization form.
Local $sJwsJson = $oJws.CreateJws()
If ($oJws.LastMethodSuccess = False) Then
    ConsoleWrite($oJws.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite($sJwsJson & @CRLF)