Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJws
    Variant vHeader
    Handle hoHeader
    Boolean iBIncludeBom
    Variant vBdKey
    Handle hoBdKey
    String sJwsCompact
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    //  Protected header specifying HMAC-SHA256.
    Get Create (RefClass(cComChilkatJsonObject)) To hoHeader
    If (Not(IsComObjectCreated(hoHeader))) Begin
        Send CreateComObject of hoHeader
    End
    Get ComUpdateString Of hoHeader "alg" "HS256" To iSuccess
    Get pvComObject of hoHeader to vHeader
    Get ComSetProtectedHeader Of hoJws 0 vHeader To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Move False To iBIncludeBom
    Get ComSetPayload Of hoJws "This is the content to sign." "utf-8" iBIncludeBom To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Get Create (RefClass(cComChilkatBinData)) To hoBdKey
    If (Not(IsComObjectCreated(hoBdKey))) Begin
        Send CreateComObject of hoBdKey
    End
    Get ComAppendEncoded Of hoBdKey "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=" "base64" To iSuccess
    Get pvComObject of hoBdKey to vBdKey
    Get ComSetMacKeyBd Of hoJws 0 vBdKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCreateJws Of hoJws To sJwsCompact
    Get ComLastMethodSuccess Of hoJws To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoJws To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sJwsCompact


End_Procedure