Tcl
Tcl
Sign a JWS with an HMAC Key from BinData
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.
Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.
Chilkat Tcl Downloads
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 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
exit
}
# Set the symmetric MAC key for signature 0 from the raw bytes in a BinData. In production, obtain
# the key material from a secure source.
set bdKey [new_CkBinData]
CkBinData_AppendEncoded $bdKey "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=" "base64"
set success [CkJws_SetMacKeyBd $jws 0 $bdKey]
if {$success == 0} then {
puts [CkJws_lastErrorText $jws]
delete_CkJws $jws
delete_CkJsonObject $header
delete_CkBinData $bdKey
exit
}
set jwsCompact [CkJws_createJws $jws]
if {[CkJws_get_LastMethodSuccess $jws] == 0} then {
puts [CkJws_lastErrorText $jws]
delete_CkJws $jws
delete_CkJsonObject $header
delete_CkBinData $bdKey
exit
}
puts "$jwsCompact"
delete_CkJws $jws
delete_CkJsonObject $header
delete_CkBinData $bdKey