需要像在 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
我有这个目标-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 编码,但它不起作用。
任何建议都是非常受欢迎的。
I need to generate a hash using HMAC SHA256. I am using the following code in Java. I need an equivalent code in 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();
Key = YARJSuwP5Oo6/r47LczzWjUx/T8ioAJpUK2YfdI/ZshlTUP8q4ujEVjC0seEUAAtS6YEE1Veghz+IDbNQb+2KQ==
Value =
id=456|time=19:10|nonce=8
Output =
4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c
I have this Objective-C function:
//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
The output that I am getting is this:
8736bc4aa7fc3aa071f2b4262b6972a89d2861559a20afa765e46ff17cb181a9
I tried removing the base64 encoding, but it didn't work.
Any suggestions are most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要修复您的 Java hmac 打印机,因为
4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c
不是 有效的。所有这些ffffff
都表明您在将字节转换为十六进制之前将它们符号扩展为 32 位有符号整数。正确的 hmac 大概是4ed8ce7cc4c71b2f72dc21a1e0e62d32550b0771296b9c1159de86759928654c
。无论如何,我怀疑你错误地调用了你的方法。我将您的代码复制到一个测试程序中,该程序为您的密钥和数据提供了以下输出:
与您所需的输出匹配(符号扩展错误除外)。
这是我的测试程序:
You need to fix your Java hmac printer, because
4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c
isn't valid. All thoseffffff
in there are a giveaway that you are sign-extending the bytes to 32-bit signed integers before converting them to hex. Presumably the correct hmac is4ed8ce7cc4c71b2f72dc21a1e0e62d32550b0771296b9c1159de86759928654c
.Anyway, I suspect you are calling your method incorrectly. I copied your code into a test program which gives me this output for your key and data:
That matches your desired output (except for the sign-extension errors).
Here's my test program:
该行
有一个错误
如果你的密钥是 0x00 字节
strlen(cKey)
会给出错误的长度,并且 hmac 生成过程会产生一些垃圾。在我的实现中,我已将其更改为:
Line
has a bug
If in your key would be 0x00 byte
strlen(cKey)
would give wrong length and hmac generation process will produce some rubbish.In my implementation I have changed that to: