封装一个类
.h里
#import@interface NSString (md5string)
+ (NSString *)md5String:(NSString *)str;
@end
.m里
#import "NSString+md5string.h"#import@implementation NSString (md5string)
+ (NSString *)md5String:(NSString *)str
{
const char *myPasswd = [str UTF8String];
unsigned char mdc[16];
CC_MD5(myPasswd, (CC_LONG)strlen(myPasswd), mdc);
NSMutableString *md5String = [NSMutableString string];
for (int i = 0; i< 16; i++) {
[md5String appendFormat:@"%02x",mdc[i]];
}
return md5String;
}
@end
运用
NSString *string = @"aaaa";
NSString *md5Str = [NSString md5String:string];
网友评论