Sample code for 30+ languages & platforms
Tcl

Validate a JWS with a Public Key

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetPublicKey, which sets the public key used to validate a signature of a loaded JWS.

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. The public key verifies a signature created with the corresponding private key. Validate returns 1 when valid, 0 when not, or a negative value on error.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set jws [new_CkJws]

#  Load the JWS compact serialization.
set jwsCompact "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
set success [CkJws_LoadJws $jws $jwsCompact]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    exit
}

#  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 signer's public key and set it for validating signature 0.
set pubKey [new_CkPublicKey]

set success [CkPublicKey_LoadFromFile $pubKey "qa_data/signer_pubkey.pem"]
if {$success == 0} then {
    puts [CkPublicKey_lastErrorText $pubKey]
    delete_CkJws $jws
    delete_CkPublicKey $pubKey
    exit
}

set success [CkJws_SetPublicKey $jws 0 $pubKey]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkPublicKey $pubKey
    exit
}

#  Validate the signature.  Validate returns 1 when valid, 0 when not, or a negative value on error.
set v [CkJws_Validate $jws 0]
if {$v < 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkJws $jws
    delete_CkPublicKey $pubKey
    exit
}

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

puts "The signature is valid."

delete_CkJws $jws
delete_CkPublicKey $pubKey