在项目中需要使用MD5加密,苹果提供了处理MD5的方法,我们只需要引用它的框架即可完成加密操作.
我们需要引用的头文件是
#import <CommonCrypto/CommonDigest.h>
接下来,我们只需要在我们代码中加入下面这个方法:
- (NSString *)md5:(NSString *)str
{
const char *cStr = [str UTF8String];
unsigned char result[16];
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加密后的字符串.