Created
May 16, 2014 03:09
-
-
Save shuizhongyueming/8a0b888b4abc27a3821e to your computer and use it in GitHub Desktop.
实现md5加密的方案
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(NSString *)md5:(NSString *)str { | |
//转换成utf-8 | |
const char *cStr = [str UTF8String]; | |
//开辟一个16字节(128位:md5加密出来就是128位/bit)的空间(一个字节=8字位=8个二进制数) | |
unsigned char result[16]; | |
CC_MD5( cStr, strlen(cStr), result); | |
/* | |
extern unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md)官方封装好的加密方法 | |
把cStr字符串转换成了32位的16进制数列(这个过程不可逆转) 存储到了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] | |
]; | |
/* | |
x表示十六进制,%02X 意思是不足两位将用0补齐,如果多余两位则不影响 | |
NSLog("%02X", 0x888); //888 | |
NSLog("%02X", 0x4); //04 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment