我尝试使用 md5 散列字符串(十六进制)。然而,输出字符串与我在 python 或 javascript (node.js) 中创建的类似输出不匹配。
输入字符串:
NSString *input = @"001856413a840871624d6c6f553885b5b16fae345c6bdd44afb26141483bff629df47dbd9ad0";
- (NSString *)md5:(NSString *)input {
// b2b22e766b849b8eb41b22ae85dc49b1
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
const char *cStr = [input UTF8String];
CC_MD5_CTX ctx;
CC_MD5_Init(&ctx);
CC_MD5_Update(&ctx, cStr, [input length]);
CC_MD5_Final(md5Buffer, &ctx);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x",md5Buffer[i]];
return output;
在 python 中我这样做:
binaryCredentialString = binascii.unhexlify(input)
# Now MD5 hash the binary string
m = hashlib.md5()
m.update(binaryCredentialString)
# Hex encode the hash to obtain the final credential string
credential = m.hexdigest()
Python 返回正确的 md5 输出,object-C 没有。这是为什么?
非常感谢任何帮助 - 帕斯卡
请您参考如下方法:
在 Python 中,您正在将十六进制解码回二进制,但在 Objective-C 中则不是。将您的字符串解码为 NSMutableData 实例,然后向摘要函数提供 [data bytes](和 [data length])。
