Sample code for 30+ languages & platforms
Tcl

Load and Validate a JWS from a StringBuilder

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.LoadJwsSb, which loads a JWS from a StringBuilder (here read from a file). The example then supplies the MAC key and validates the signature.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Validate returns 1 when the signature or MAC is cryptographically valid, 0 when it is not, or a negative value on error.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set jws [new_CkJws]

#  The file path is relative to the application's current working directory.  An absolute path may
#  also be used.  Supply the path appropriate to your own environment.

#  Load the JWS text from a file into a StringBuilder.
set sbJws [new_CkStringBuilder]

set bLoaded [CkStringBuilder_LoadFile $sbJws "qa_data/message.jws" "utf-8"]
if {$bLoaded != 1} then {
    puts "Failed to load the JWS file."
    delete_CkJws $jws
    delete_CkStringBuilder $sbJws
    exit
}

#  Load the JWS from the StringBuilder.
set success [CkJws_LoadJwsSb $jws $sbJws]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkStringBuilder $sbJws
    exit
}

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

set v [CkJws_Validate $jws 0]
if {$v < 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkStringBuilder $sbJws
    exit
}

if {$v != 1} then {
    puts "The signature is not valid."
    delete_CkJws $jws
    delete_CkStringBuilder $sbJws
    exit
}

puts "The signature is valid."

delete_CkJws $jws
delete_CkStringBuilder $sbJws