变更记录
序号 | 录入时间 | 录入人 | 备注 |
---|---|---|---|
1 | 2015-02-27 | Alfred Jiang | - |
2 | 2015-12-22 | Alfred Jiang | - |
方案名称
数据加密 - MD5 加密
关键字
数据加密 \ 加密 \ 解密 \ MD5 \ 摘要算法
需求场景
- 对信息有特殊的 MD5 摘要算法需求
参考链接
详细内容
NSString+MyAdditions.h
@interface NSString (MyAdditions)
- (NSString *)md5;
@end
NSData+MyAdditions.h
@interface NSData (MyAdditions)
- (NSString*)md5;
@end
NSString+MyAdditions.m
#import "MyAdditions.h"
#import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
@implementation NSString (MyAdditions)
- (NSString *)md5
{
const char *cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
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]
];
}
@end
NSData+MyAdditions.m
@implementation NSData (MyAdditions)
- (NSString*)md5
{
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( self.bytes, self.length, result ); // This is the md5 call
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]
];
}
@end
效果图
(无)
备注
(无)