Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
string ls_KeyHex
string ls_MessageBytes
oleobject loo_Bd
string ls_Cmac
string ls_PlainText
string ls_EncTag

li_Success = 0

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

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Set the MAC algorithm to AES-CMAC
loo_Crypt.MacAlgorithm = "aes-cmac"

// AES-CMAC always uses a 16-byte (128-bit) MAC key.
ls_KeyHex = "2b7e151628aed2a6abf7158809cf4f3c"
li_Success = loo_Crypt.SetMacKeyEncoded(ls_KeyHex,"hex")

// 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.
ls_MessageBytes = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

loo_Bd.AppendEncoded(ls_MessageBytes,"hex")

// Compute the AES-CMAC for the bytes contained in bd and return the AES-CMAC in hex representation.
loo_Crypt.EncodingMode = "hex_lower"
ls_Cmac = loo_Crypt.MacBdENC(loo_Bd)
Write-Debug ls_Cmac
// Output should be: 51f0bebf7e3b9d92fc49741779363cfe

// Now do the same for a string:
ls_PlainText = "'Twas brillig, and the slithy toves~nDid gyre and gimble in the wabe:~nAll mimsy were the borogoves,~nAnd the mome raths outgrabe."
ls_EncTag = loo_Crypt.MacStringENC(ls_PlainText)
Write-Debug ls_EncTag

// Output should be: 4fca1fcbd265048d247f99ab57fa3ceb


destroy loo_Crypt
destroy loo_Bd