Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

set jws [new_CkJws]

#  Protected header specifying HMAC-SHA256.
set header [new_CkJsonObject]

CkJsonObject_UpdateString $header "alg" "HS256"
set success [CkJws_SetProtectedHeader $jws 0 $header]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkJsonObject $header
    exit
}

#  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.
set unprotected [new_CkJsonObject]

CkJsonObject_UpdateString $unprotected "kid" "signer-key-1"
set success [CkJws_SetUnprotectedHeader $jws 0 $unprotected]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkJsonObject $header
    delete_CkJsonObject $unprotected
    exit
}

set bIncludeBom 0
set success [CkJws_SetPayload $jws "This is the content to sign." "utf-8" $bIncludeBom]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkJsonObject $header
    delete_CkJsonObject $unprotected
    exit
}

set base64Key "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
set success [CkJws_SetMacKey $jws 0 $base64Key "base64"]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkJsonObject $header
    delete_CkJsonObject $unprotected
    exit
}

#  Because an unprotected header is present, the JWS is produced in a JSON serialization form.
set jwsJson [CkJws_createJws $jws]
if {[CkJws_get_LastMethodSuccess $jws] == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkJsonObject $header
    delete_CkJsonObject $unprotected
    exit
}

puts "$jwsJson"

delete_CkJws $jws
delete_CkJsonObject $header
delete_CkJsonObject $unprotected