Created
February 18, 2014 04:26
-
-
Save phnessu4/9064639 to your computer and use it in GitHub Desktop.
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
#import <Foundation/Foundation.h> | |
@interface MD5 : NSObject | |
/** | |
* @brief calculate a message-digest fingerprint for a input string. | |
* | |
* @return return a message-digest fingerprint | |
*/ | |
+ (NSString *)MD5String:(NSString *)string; | |
+ (NSString *)MD5Data:(NSData *)data; //获取某个文件(文件先转为 NSData)的 MD5值 | |
@end |
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
#import "MD5.h" | |
#import <CommonCrypto/CommonDigest.h> | |
@implementation MD5 | |
+ (NSString *)MD5String:(NSString *)string | |
{ | |
if (string == nil || [string length] == 0) { | |
return nil; | |
} | |
const char *value = [string UTF8String]; | |
unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH]; | |
CC_MD5(value, strlen(value), outputBuffer); | |
NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; | |
for (NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++) { | |
[outputString appendFormat:@"%02x",outputBuffer[count]]; | |
} | |
return outputString; | |
} | |
+ (NSString *)MD5Data:(NSData *)data | |
{ | |
if (data == nil || [data length] == 0) { | |
return nil; | |
} | |
unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH]; | |
CC_MD5([data bytes], [data length], outputBuffer); | |
NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; | |
for (NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++) { | |
[outputString appendFormat:@"%02x",outputBuffer[count]]; | |
} | |
return outputString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment