Sample code for 30+ languages & platforms
DataFlex

AES-CMAC

See more Encryption Examples

Demonstrates using the AES-CMAC algorithm, which is a keyed hash function similar to HMAC and Poly1305.

Note: Chilkat added AES-CMAC in version 9.5.0.95.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCrypt
    String sKeyHex
    String sMessageBytes
    Variant vBd
    Handle hoBd
    String sCmac
    String sPlainText
    String sEncTag

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End

    // Set the MAC algorithm to AES-CMAC
    Set ComMacAlgorithm Of hoCrypt To "aes-cmac"

    // AES-CMAC always uses a 16-byte (128-bit) MAC key.
    Move "2b7e151628aed2a6abf7158809cf4f3c" To sKeyHex
    Get ComSetMacKeyEncoded Of hoCrypt sKeyHex "hex" To iSuccess

    // Let's compute the AES-CMAC for the test vector at https://www.rfc-editor.org/rfc/rfc4493#appendix-A
    // Here we have 64 bytes in hex representation.
    Move "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710" To sMessageBytes

    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComAppendEncoded Of hoBd sMessageBytes "hex" To iSuccess

    // Compute the AES-CMAC for the bytes contained in bd and return the AES-CMAC in hex representation.
    Set ComEncodingMode Of hoCrypt To "hex_lower"
    Get pvComObject of hoBd to vBd
    Get ComMacBdENC Of hoCrypt vBd To sCmac
    Showln sCmac
    // Output should be: 51f0bebf7e3b9d92fc49741779363cfe

    // Now do the same for a string:
    Move "'Twas brillig, and the slithy toves" + (character(10)) + "Did gyre and gimble in the wabe:" + (character(10)) + "All mimsy were the borogoves," + (character(10)) + "And the mome raths outgrabe." To sPlainText
    Get ComMacStringENC Of hoCrypt sPlainText To sEncTag
    Showln sEncTag

    // Output should be: 4fca1fcbd265048d247f99ab57fa3ceb


End_Procedure