写法1.
#pragma mark - 16位加密小写
-(NSString *)md5:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5(cStr, strlen(cStr), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i++)
{
[hash appendFormat:@"%02X", result[i]];
}
return [hash lowercaseString];
}
写法2.
//md5 32位 加密 (小写)
+ (NSString *)md5:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[32];
CC_MD5( cStr, strlen(cStr), result );
return [NSString stringWithFormat:
}
以上写法自己试过,但是网上有看到了这个写法,里面有点评
- (NSString *)getMd5_32Bit_String:(NSString *)srcString{
constchar *cStr = [srcString UTF8String];
unsignedchar digest[CC_MD5_DIGEST_LENGTH];
// CC_MD5( cStr, strlen(cStr), digest ); 这里的用法明显是错误的,但是不知道为什么依然可以在网络上得以流传。当srcString中包含空字符(\0)时
CC_MD5( cStr, self.length, digest );
NSMutableString *result = [NSMutableStringstringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for
(
int
i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[result appendFormat:@
"%02x"
, digest[i]];
return
result;
}
|
算法简介:
转载自:http://www.2cto.com/kf/201410/340840.html