I am trying to generate hmac SHA256 in objective C in the following way:
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
This works fine as long as the key is a string. Problem is on the external server command used to generate mac is:
openssl dgst -binary -sha256 -mac HMAC -macopt hexkey:$key
There hmac is getting generated considering key as hexkey . So obviously hmac generated is different.
How do i tell the CCMac function in objective C to consider the key as hex key? I already tried converting the string key to byte array and passing it to CCMAC but still didn't work.
In android I have achieved the same by converting the hex number to a Big Integer and then doing getBytes on it, use it to create the secret key. Hope some of you will be able to guide me.