Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

$oJws = ObjCreate("Chilkat.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.

;  Load the JWS text from a file into a StringBuilder.
$oSbJws = ObjCreate("Chilkat.StringBuilder")
Local $bLoaded = $oSbJws.LoadFile("qa_data/message.jws","utf-8")
If ($bLoaded <> True) Then
    ConsoleWrite("Failed to load the JWS file." & @CRLF)
    Exit
EndIf

;  Load the JWS from the StringBuilder.
$bSuccess = $oJws.LoadJwsSb($oSbJws)
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

Local $iV = $oJws.Validate(0)
If ($iV < 0) Then
    ConsoleWrite($oJws.LastErrorText & @CRLF)
    Exit
EndIf

If ($iV <> 1) Then
    ConsoleWrite("The signature is not valid." & @CRLF)
    Exit
EndIf

ConsoleWrite("The signature is valid." & @CRLF)