iOS开发中字符串MD5加密方法的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
- (NSString *) md5
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (unsigned int) 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]
                        ];      
}

记得要加上头文件#import <CommonCrypto/CommonDigest.h>否则会有警告

错误提示:implicit declaration of function ‘CC_MD5’ is invalid in C99,这个错误提示的原因是使用了c语言库的CC_MD5函数而没有包含相关的头文件,因此只需要引入相关头文件即可解除警告。

Leave a Reply