Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJws
    Variant vSbJws
    Handle hoSbJws
    Boolean iBLoaded
    String sBase64Key
    Integer v
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatJws)) To hoJws
    If (Not(IsComObjectCreated(hoJws))) Begin
        Send CreateComObject of hoJws
    End

    //  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.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbJws
    If (Not(IsComObjectCreated(hoSbJws))) Begin
        Send CreateComObject of hoSbJws
    End
    Get ComLoadFile Of hoSbJws "qa_data/message.jws" "utf-8" To iBLoaded
    If (iBLoaded <> True) Begin
        Showln "Failed to load the JWS file."
        Procedure_Return
    End

    //  Load the JWS from the StringBuilder.
    Get pvComObject of hoSbJws to vSbJws
    Get ComLoadJwsSb Of hoJws vSbJws To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=" To sBase64Key
    Get ComSetMacKey Of hoJws 0 sBase64Key "base64" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComValidate Of hoJws 0 To v
    If (v < 0) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    If (v <> 1) Begin
        Showln "The signature is not valid."
        Procedure_Return
    End

    Showln "The signature is valid."


End_Procedure