简体   繁体   English

如何将十六进制字符串提供给Hmac SHA256目标C?

[英]How to give hex string to Hmac SHA256 objective C?

I am trying to generate hmac SHA256 in objective C in the following way: 我试图通过以下方式在目标C中生成hmac SHA256:

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: 问题是用于生成mac的外部服务器命令是:

openssl dgst -binary -sha256 -mac HMAC -macopt hexkey:$key 

There hmac is getting generated considering key as hexkey . 认为key为hexkey的 hmac正在生成。 So obviously hmac generated is different. 因此,显然生成的hmac是不同的。

How do i tell the CCMac function in objective C to consider the key as hex key? 如何告诉目标C中的CCMac函数将密钥视为十六进制密钥? I already tried converting the string key to byte array and passing it to CCMAC but still didn't work. 我已经尝试过将字符串键转换为字节数组,并将其传递给CCMAC,但仍然无法正常工作。

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. 在android中,我已通过将十六进制数转换为Big Integer,然后对其执行getBytes,使用它来创建密钥来实现相同目的。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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