需要像在 Java 中一样在 Objective C 中生成 HMAC SHA256 哈希 [英] Need to generate HMAC SHA256 hash in Objective C as in Java

查看:25
本文介绍了需要像在 Java 中一样在 Objective C 中生成 HMAC SHA256 哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 HMAC SHA256 生成哈希.我在 Java 中使用以下代码.我需要 Objective-C 中的等效代码.

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==

价值 =

id=456|time=19:10|nonce=8

输出=

4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c

我有这个 Objective-C 函数:

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

我尝试删除 base64 编码,但没有奏效.

I tried removing the base64 encoding, but it didn't work.

欢迎提出任何建议.

推荐答案

你需要修复你的 Java hmac 打印机,因为 4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffa1ffffffe0ffffffe62d32550b07712961ffcode5ffc8ff8ff7ff5ffc8ff8ff7ff7ff7ff7ff4ff7ff7ff4ff7ff8所有那些 ffffff 都有一个赠品,即您在将字节转换为十六进制之前将字节符号扩展为 32 位有符号整数.大概正确的 hmac 是 4ed8ce7cc4c71b2f72dc21a1e0e62d32550b0771296b9c1159de86759928654c.

You need to fix your Java hmac printer, because 4effffffd8ffffffce7cffffffc4ffffffc71b2f72ffffffdc21ffffffa1ffffffe0ffffffe62d32550b0771296bffffff9c1159ffffffdeffffff8675ffffff9928654c isn't valid. All those ffffff 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 is 4ed8ce7cc4c71b2f72dc21a1e0e62d32550b0771296b9c1159de86759928654c.

无论如何,我怀疑您错误地调用了您的方法.我将您的代码复制到一个测试程序中,该程序为我提供了您的密钥和数据的输出:

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:

2011-12-10 13:03:38.231 hmactest[8251:707] test hmac = <4ed8ce7c c4c71b2f 72dc21a1 e0e62d32 550b0771 296b9c11 59de8675 9928654c>

匹配您想要的输出(符号扩展错误除外).

That matches your desired output (except for the sign-extension errors).

这是我的测试程序:

#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 哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆