NSData 转 md5
NSData 转 md5
NSData+MD5Digest.h
#import <Foundation/Foundation.h>
@interface NSData (MD5Digest)
+(NSData *)MD5Digest:(NSData *)input;
-(NSData *)MD5Digest;
+(NSString *)MD5HexDigest:(NSData *)input;
-(NSString *)MD5HexDigest;
@end
NSData+MD5Digest.m
#import "NSData+MD5Digest.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSData (MD5)
+(NSData *)MD5Digest:(NSData *)input {
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input.bytes, (CC_LONG)input.length, result);
return [[NSData alloc] initWithBytes:result length:CC_MD5_DIGEST_LENGTH];
}
-(NSData *)MD5Digest {
return [NSData MD5Digest:self];
}
+(NSString *)MD5HexDigest:(NSData *)input {
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input.bytes, (CC_LONG)input.length, result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for (int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:@"%02x",result[i]];
}
return ret;
}
-(NSString *)MD5HexDigest {
return [NSData MD5HexDigest:self];
}
@end
源github地址: suiyi