【发布时间】:2011-09-15 05:19:32
【问题描述】:
与this question 非常相似,我希望计算一个字符串的 MD5 哈希值以用于 API。
根据this thread on Apple Discussions,这不适用于设备:
CommonCrypto 框架不在 iPhone 上。不幸的是, iPhone Simulator 针对 Mac OS X 框架进行编译,因此可以运行 模拟器......但你不会让它为设备编译。
这样的代码还能在设备上运行吗?
#import <CommonCrypto/CommonDigest.h>
#define CC_MD5_DIGEST_LENGTH 16 /* digest length in bytes */
- (NSString *)md5:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, strlen(cStr), result);
return [NSString stringWithFormat: @"%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]];
}
在设备上生成 MD5 哈希的最新和最佳方法是什么? 如何在 XCode 4 中添加 CommonCrypto/CommonDigtest.h?
谢谢,
【问题讨论】:
标签: iphone objective-c ios4 hash