iOS/Objective C:SHA-1 和 Base64
我必须将短语“1234”转换为其 SHA-1 哈希值的 Base64 编码。
我想要: '1234' = cRDtpNCeBiql5KOQsKVyrA0sAiA= (此示例与服务器通信相关)...
但是当我在这些站点上手动检查它或在我的 XCode 项目中使用我的方法时,结果总是不同:
1234
After SHA-1 (http://www.sha1.cz/) 7110eda4d09e062aa5e4a390b0a572ac0d2c0220
Base64 之后 (http://base64-encoder-online.waraxe.us/) NzExMGVkYTRkMDllMDYyYWE1ZTRhMzkwYjBhNTcyYWMwZDJjMDIyMA==
这是我的 SHA-1 函数:
- (NSString *)sha1:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(cStr, strlen(cStr), result);
NSString *s = [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3], result[4],
result[5], result[6], result[7],
result[8], result[9], result[10], result[11], result[12],
result[13], result[14], result[15],
result[16], result[17], result[18], result[19]
];
return [s lowercaseString];
}
对于 Base64,我使用此类: http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
这是SHA-1和Base64的调用:
NSString *pwHash =[self sha1:self._txtFieldPW.text]; //In my case '1234'
NSLog(@"Hash: %@",pwHash); //7110eda4d09e062aa5e4a390b0a572ac0d2c0220
//7110eda4d09e062aa5e4a390b0a572ac0d2c0220
NSData *pwHashData = [[NSData alloc]initWithData:[pwHash dataUsingEncoding:1]];
NSString *base64 = [pwHashData base64Encoding];
NSLog(@"Base64: %@",base64);
//NzExMGVkYTRkMDllMDYyYWE1ZTRhMzkwYjBhNTcyYWMwZDJjMDIyMA==
出了什么问题?
I have to convert the phrase '1234' into the Base64-encoding of its SHA-1 hash.
I want to have: '1234' = cRDtpNCeBiql5KOQsKVyrA0sAiA=
(This example work in connection with the server communication)...
But when I checked it manualley on these sites or use my Methods in my XCode Project the result is always different:
1234
After SHA-1 (http://www.sha1.cz/)
7110eda4d09e062aa5e4a390b0a572ac0d2c0220
After Base64 (http://base64-encoder-online.waraxe.us/)
NzExMGVkYTRkMDllMDYyYWE1ZTRhMzkwYjBhNTcyYWMwZDJjMDIyMA==
This is my SHA-1 function:
- (NSString *)sha1:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(cStr, strlen(cStr), result);
NSString *s = [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3], result[4],
result[5], result[6], result[7],
result[8], result[9], result[10], result[11], result[12],
result[13], result[14], result[15],
result[16], result[17], result[18], result[19]
];
return [s lowercaseString];
}
For Base64 I use a this class: http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
This is the call of SHA-1 and Base64:
NSString *pwHash =[self sha1:self._txtFieldPW.text]; //In my case '1234'
NSLog(@"Hash: %@",pwHash); //7110eda4d09e062aa5e4a390b0a572ac0d2c0220
//7110eda4d09e062aa5e4a390b0a572ac0d2c0220
NSData *pwHashData = [[NSData alloc]initWithData:[pwHash dataUsingEncoding:1]];
NSString *base64 = [pwHashData base64Encoding];
NSLog(@"Base64: %@",base64);
//NzExMGVkYTRkMDllMDYyYWE1ZTRhMzkwYjBhNTcyYWMwZDJjMDIyMA==
What is going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈希值是一个二进制值。 “cRDtpNCeBiql5KOQsKVyrA0sAiA=”字符串是哈希编码的二进制值的结果。同时,“NzExMGVkYTRkMDllMDYyYWE1ZTRhMzkwYjBhNTcyYWMwZDJjMDIyMA==”字符串是对十六进制哈希值进行Base64转换的结果。
这有道理吗?所以你有一个 SHA1 哈希值,一个 20 字节的数组:{0x71, 0x10, ...}。您可以按原样对该内存块进行 Base64 编码。或者您可以将每个字节转换为两个小写十六进制数字,然后您将得到 ASCII 字符串“7110eda4d09e062aa5e4a390b0a572ac0d2c0220”。如果您将 Base64 编码应用于该字符串,而不是原始哈希字节,您将获得 Base64 值“NzExMGV...”。
编辑,具有额外的二进制优点:重新编写第二个片段:
并摆脱
sha1
方法。一旦删除了可怕的stringWithFormat
调用,它就变成了两行代码。如果您确实想要日志中的十六进制哈希值,请成为我的客人;但不要将十六进制字符串视为哈希的真实值,因为它不是。除非您使用 ARC,否则不要忘记随后释放
NSData
对象。Hash is a binary value. The "cRDtpNCeBiql5KOQsKVyrA0sAiA=" string is a result of the binary value of the hash, encoded. Meanwhile, the "NzExMGVkYTRkMDllMDYyYWE1ZTRhMzkwYjBhNTcyYWMwZDJjMDIyMA==" string is the result of the Base64 transform on the hexadecimal presentation of the hash.
Did that make sense? So you have a SHA1 hash, an array of 20 bytes: {0x71, 0x10, ...}. You can Base64-encode that chunk of memory as it is. Or you can convert each byte into two lowercase hex digits, then you'll get the ASCII string "7110eda4d09e062aa5e4a390b0a572ac0d2c0220". If you apply Base64 encoding to that string, as opposed to the original hash bytes, you'll get the Base64 value "NzExMGV...".
EDIT, with extra binary goodness: reformulate the second snippet thus:
And get rid of the
sha1
method. Once you remove the monstrousstringWithFormat
call, it's a two-liner. If you really want the hex hash in the log, be my guest; but don't treat the hex string as the true value of the hash, 'cause it's not.Unless you're using ARC, don't forget to free the
NSData
object afterwards.