简体   繁体   中英

How to give hex string to Hmac SHA256 objective C?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM