HMAC SHA1 of Byte Array in “C” Programming Language

Here is a “C” programming language code snippet compute the HMAC SHA1 using byte arrays.

void demonstrate_hmac_sha1_binary_bytes()
{
    unsigned char key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56 };
    unsigned char data[] = { 0x90, 0x00, 0x00, 0x1f, 0x45, 0x81, 0x10, 0x00, 0x00, 0x80, 0x10, 0x00, 0x10, 0x07, 0x10, 0x04 };
    HCkCrypt2 crypt;
    HCkByteData mac;
    HCkByteData keyBytes;
    int i;
    int szMac;
    const unsigned char *macBytes;

    keyBytes = CkByteData_Create();
    CkByteData_append2(keyBytes,key,sizeof(key));


    crypt = CkCrypt2_Create();
    CkCrypt2_putMacAlgorithm(crypt,"hmac");
    CkCrypt2_putHashAlgorithm(crypt,"sha1");
    CkCrypt2_SetMacKeyBytes(crypt,keyBytes);

    mac = CkByteData_Create();
    CkCrypt2_MacBytes2(crypt,data,sizeof(data),mac);

    szMac = CkByteData_getSize(mac);
    macBytes = CkByteData_getData(mac);

    for (i=0; i<szMac; i++)
    {
        printf("%x ",macBytes[i]);
    }
    printf("\n");

    CkCrypt2_Dispose(crypt);
    CkCrypt2_Dispose(mac);
    CkCrypt2_Dispose(keyBytes);
}
Tags :