Skip to content

Instantly share code, notes, and snippets.

@ktakayama
Created March 30, 2009 09:00
Show Gist options
  • Save ktakayama/87697 to your computer and use it in GitHub Desktop.
Save ktakayama/87697 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import "CommonCrypto/CommonDigest.h"
@interface NSString (MD5)
- (NSString *) MD5String;
@end
@implementation NSString (MD5)
- (NSString *) MD5String {
const char *cStr = [self UTF8String];
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, strlen(cStr), digest );
char md5string[CC_MD5_DIGEST_LENGTH*2];
int i;
for (i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
sprintf(md5string+i*2, "%02X", digest[i]);
}
return [NSString stringWithCString:md5string length:CC_MD5_DIGEST_LENGTH*2];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment