HmacSHA256 Objective-C 加密

发布于 2024-12-08 23:03:48 字数 1024 浏览 0 评论 0原文

我想使用 HmacSHA256 用密钥加密一个字符串。每个人都使用的代码是下面的代码,但有一点没有意义。 如果我们想要的只是 HmacSHA256 哈希值,为什么最后要使用 base64?

我尝试查看调用 CCHmac 方法后生成的哈希

NSString *str = [[NSString alloc] initWithData:HMAC encoding:NSASCIIStringEncoding];
NSLog(@"%@", str);

但我没有生成生成的哈希,我得到 null 或垃圾,如下所示:

2011-10-11 09:38:05.082 Hash_HmacSHA256[368:207] (无效的) 2011-10-11 09:38:05.085 Hash_HmacSHA256[368:207] Rwªb7iså{yyþ§Ù(&oá÷ÛËÚ¥M`f

import < CommonCrypto/CommonHMAC.h>

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

NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
                                  length:sizeof(cHMAC)];

NSString *hash = [HMAC base64Encoding]; //This line doesn´t make sense

[key release];
[data release];

I wanna encpryt a string with a key, using HmacSHA256. The code everyone use is the one below, but there is one thing that doesn´t make sense.
Why would we use base64 at the end if all we want is the HmacSHA256 hash?

I tried seeing the hash generated after the method CCHmac is called with

NSString *str = [[NSString alloc] initWithData:HMAC encoding:NSASCIIStringEncoding];
NSLog(@"%@", str);

But i don´t get the hash generated, i get null, or garbage, like this:

2011-10-11 09:38:05.082 Hash_HmacSHA256[368:207] (null)
2011-10-11 09:38:05.085 Hash_HmacSHA256[368:207] Rwªb7iså{yyþ§Ù(&oá÷ÛËÚ¥M`f

import < CommonCrypto/CommonHMAC.h>

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

NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
                                  length:sizeof(cHMAC)];

NSString *hash = [HMAC base64Encoding]; //This line doesn´t make sense

[key release];
[data release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骄傲 2024-12-15 23:03:48

首先,对于那些想知道的人,这是参考我对这个问题的回答: HMAC-SHA1 的 Objective-C 示例代码


您生成的 HMAC 是一个 256 位二进制值,可能以也可能不以 0 字节开头。

为了能够打印它,您需要一个字符串表示形式(二进制、十六进制、十进制、base64 等)。 Base64 是其中最有效的一种,这就是我在那里使用 Base64 编码的原因。

出现垃圾的原因是 HMAC 值中的大多数(如果不是全部)八位字节都超出了可打印 ASCII 字符的范围。如果第一个八位字节是 0 (0x00),则得到 nil。这就是为什么您需要支持任意值的编码。 ASCII 没有。

当然,如果你不想打印HMAC值,那么可能不需要这样的编码,并且可以保持HMAC原样(二进制NSData)。

First of all, for those wondering, this is in reference to my answer to this question: Objective-C sample code for HMAC-SHA1


The HMAC you generate is a 256-bit binary value that may or may not start with a 0 byte.

To be able to print it, you need a string representation (binary, hex, decimal, base64, etc.). Base64 is one of the most efficient among these, that's why I used a Base64 encoding there.

The reason you get garbage is that most (if not all) of the octets in the HMAC value are outside the range of printable ASCII characters. If the first octet is 0 (0x00), you get nil. This is why you need an encoding that supports arbitrary values. ASCII doesn't.

Of course, if you don't want to print the HMAC value, then may not need such an encoding, and can keep the HMAC as is (binary NSData).

怼怹恏 2024-12-15 23:03:48

我花了一整天的时间,试图将生成的哈希(字节)转换为可读的数据。我使用了你说的base64编码,但它对我来说根本不起作用。

所以我做的是这样的:

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:cHMAC length:CC_SHA256_DIGEST_LENGTH];

// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
// hash is now a string with just the 40char hash value in it
NSLog(@"%@",hash);

I spend a whole day, trying to convert the generated hash (bytes) into readable data. I used the base64 encoded you said and it didn´t work at all for me .

So what i did was this:

CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:cHMAC length:CC_SHA256_DIGEST_LENGTH];

// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
// hash is now a string with just the 40char hash value in it
NSLog(@"%@",hash);
几味少女 2024-12-15 23:03:48

不要执行“[out description]”来获取字符串形式的哈希值。

执行 [hash base64Encoding] 来获取它的 base64 编码。使用 http:// cybersam.com/ios-dev/http-basic-access-authentication-with-objective-c-and-ios/attachment/nsdataadditions 获取 base64Encoding 函数。 additions 类是将函数 base64Encoding 添加到 NSData 的实现中的类别。

或者你可以执行[[NSString alloc]initWithData:out编码:NSUTF8StringEncoding]。

Don't do "[out description]" to get the hash as a string.

Do [hash base64Encoding] to get the base64 encoding of it. Use http://cybersam.com/ios-dev/http-basic-access-authentication-with-objective-c-and-ios/attachment/nsdataadditions to get the base64Encoding function. The additions class is a category that will add the function base64Encoding to NSData's implementation.

Or you can do [[NSString alloc]initWithData:out encoding:NSUTF8StringEncoding].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文