0
votes

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.

1
What is "hexkey" if it's not a string?DonMag
Its a hexadecimal string , eg:3b7fc8987611a158e561a35cba001fd6.Ankit Khare
Sorry, confused... Do you want to use Objective-C? And you want to know how to get the same results as openssl?DonMag

1 Answers

3
votes

Is this what you're looking for?

- (NSData *)dataFromHexString:(NSString *)sHex {
    const char *chars = [sHex UTF8String];
    int i = 0;
    NSUInteger len = sHex.length;

    NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
    char byteChars[3] = {'\0','\0','\0'};
    unsigned long wholeByte;

    while (i < len) {
        byteChars[0] = chars[i++];
        byteChars[1] = chars[i++];
        wholeByte = strtoul(byteChars, NULL, 16);
        [data appendBytes:&wholeByte length:1];
    }

    return data;
}

- (NSData *)hmacForHexKey:(NSString *)hexkey andStringData:(NSString *)data
{

    NSData *keyData = [self dataFromHexString:hexkey];

    const char *cKey  = [keyData bytes];
    const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];

    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

    CCHmac(kCCHmacAlgSHA256, cKey, keyData.length, cData, strlen(cData), cHMAC);

    return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

}

- (NSData *)hmacForKey:(NSString *)key andStringData:(NSString *)data
{

    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);

    return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];

}

- (void)testIt {

    NSString *key = @"This is my random key.";
    NSString *hexKey = @"54686973206973206d792072616e646f6d206b65792e";

    NSString *data = @"This is a data string.";

    NSData *hmac1 = [self hmacForKey:key andStringData:data];
    NSLog(@"hmacForKey   : %@", hmac1);

    NSData *hmac2 = [self hmacForHexKey:hexKey andStringData:data];
    NSLog(@"hmacForHexKey: %@", hmac2);
}