需要像Java中那样在Objective C中生成HMAC SHA256哈希
我需要使用HMAC SHA256生成哈希。我在Java中使用以下代码。我需要在Objective-C中使用等效的代码。
javax.crypto.Mac mac = javax.crypto.Mac.getInstance(type);javax.crypto.spec.SecretKeySpec secret = new javax.crypto.spec.SecretKeySpec(key.getBytes(), type);
mac.init(secret);
byte[] digest = mac.doFinal(value.getBytes());
StringBuilder sb = new StringBuilder(digest.length * 2);
String s="";
for (byte b: digest) {
s = Integer.toHexString(b);
if (s.length() == 1) {
sb.append('0');
}
sb.append(s);
}
return sb.toString();
键=
YARJSuwP5Oo6/r47LczzWjUx/T8ioAJpUK2YfdI/ZshlTUP8q4ujEVjC0seEUAAtS6YEE1Veghz+IDbNQb+2KQ==
值=
id=456|time=19:10|nonce=8
输出=
4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c
我有这个Objective-C功能:
//Hash method Definition - (NSString *)getHashEncription:(NSString *)key andData:(NSString *)data{
NSLog(@"Secret Key %@ And Data %@", key, data);
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
//HmacSHA256
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
length:sizeof(cHMAC)];
[Base64 initialize];
NSString *b64EncStr = [Base64 encode:HMAC];
NSLog(@"Base 64 encoded = %@",b64EncStr);
NSLog(@"NSData Value %@", HMAC);
// unsigned char hashedChars[32];
// NSString *inputString;
// inputString = [NSString stringWithFormat:@"hello"];
// NSData * inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
// CC_SHA256(inputData.bytes, inputData.length, hashedChars);
return [[NSString alloc] initWithData:HMAC encoding:NSASCIIStringEncoding];
}//End of getHashEncription
我得到的输出是这样的:
8736bc4aa7fc3aa071f2b4262b6972a89d2861559a20afa765e46ff17cb181a9
我尝试删除base64编码,但是没有用。
任何建议都是最欢迎的。
回答:
您需要修复Java
hmac打印机,因为4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c
它无效。里面的所有内容ffffff
都有一个赠品,您要先将字节符号扩展为32位带符号整数,然后再将它们转换为十六进制。大概正确的hmac是4ed8ce7cc4c71b2f72dc21a1e0e62d32550b0771296b9c1159de86759928654c
。
无论如何,我怀疑您在错误地调用您的方法。我将您的代码复制到一个测试程序中,该程序为您提供了密钥和数据的输出:
2011-12-10 13:03:38.231 hmactest[8251:707] test hmac = <4ed8ce7c c4c71b2f 72dc21a1 e0e62d32 550b0771 296b9c11 59de8675 9928654c>
匹配您所需的输出(符号扩展错误除外)。
这是我的测试程序:
#import <Foundation/Foundation.h>#import <CommonCrypto/CommonHMAC.h>
NSData *hmacForKeyAndData(NSString *key, NSString *data)
{
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
}
int main (int argc, const char * argv[])
{
@autoreleasepool {
// Compare to http://en.wikipedia.org/wiki/HMAC#Examples_of_HMAC_.28MD5.2C_SHA1.2C_SHA256_.29
NSLog(@"empty hmac = %@", hmacForKeyAndData(@"", @""));
NSLog(@"test hmac = %@", hmacForKeyAndData(@"YARJSuwP5Oo6/r47LczzWjUx/T8ioAJpUK2YfdI/ZshlTUP8q4ujEVjC0seEUAAtS6YEE1Veghz+IDbNQb+2KQ==", @"id=456|time=19:10|nonce=8"));
}
return 0;
}
以上是 需要像Java中那样在Objective C中生成HMAC SHA256哈希 的全部内容, 来源链接: utcz.com/qa/400495.html