Objective-C SHA2 哈希无法与非 ASCII 正常工作
我正在使用 xcode,这是我的 sha512 方法:
-(NSString*) sha512:(NSString*)input
{
const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.length];
uint8_t digest[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", digest[i]];
}
return output;
}
当我尝试通过输入“test”时,它返回: “ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff” 它与其他 sha512 哈希工具(包括我的 Java 程序和“http://hash.online-convert.com/sha512-generator”)相匹配。
但是,当我输入“é”等非 ascii 字符时,它返回的内容与我所有其他 sha512 工具不同。 对于输入“é”,我的方法返回: “60313f8521d3016916d876f7ad11cf42a30dfd4ff9bc557f1e2f90e0d37c56b7 6ab5e42c8a16db20c18086b0d769c08542429c262cc21ee4fba02bfc689a4797“ 当其他工具(再次包括我的 Java 程序和“http://hash.online-convert.com/sha512-generator”)返回时“9e2ad28633f24451bd4f3c1cb20586a21a44c3aeedbdc01b9cc8fa72917ea7bd689c82b8bf1fef89b911cf8cc46fa2c1ccc10087b2094fd4d3350ecd88526a2c”。
我错过了什么吗?对此有什么想法吗? 谢谢!
I am using xcode and this is my sha512 method:
-(NSString*) sha512:(NSString*)input
{
const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.length];
uint8_t digest[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", digest[i]];
}
return output;
}
When I try to pass the input "test", it returns:
"ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"
which matches other sha512 hash tools (including my Java program and "http://hash.online-convert.com/sha512-generator").
However, when I input non-ascii char like "é", it returns something different than all my other sha512 tools.
For input "é", my method returns:
"60313f8521d3016916d876f7ad11cf42a30dfd4ff9bc557f1e2f90e0d37c56b76ab5e42c8a16db20c18086b0d769c08542429c262cc21ee4fba02bfc689a4797"
when other tools (again including my Java program and "http://hash.online-convert.com/sha512-generator") return "9e2ad28633f24451bd4f3c1cb20586a21a44c3aeedbdc01b9cc8fa72917ea7bd689c82b8bf1fef89b911cf8cc46fa2c1ccc10087b2094fd4d3350ecd88526a2c".
Did I miss anything? Any ideas about this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样创建你的
NSData
对象:只需仔细检查,它就可以正常工作。
Create your
NSData
object like this:Just double checked and it works correctly like that.