Skip to content

Instantly share code, notes, and snippets.

@lilyball
Created January 24, 2009 22:25
Show Gist options
  • Save lilyball/51578 to your computer and use it in GitHub Desktop.
Save lilyball/51578 to your computer and use it in GitHub Desktop.
MD5 hash of NSData
#import <Foundation/Foundation.h>
@interface NSData (NSData_MD5)
- (NSString *)md5;
@end
#import "NSData_MD5.h"
#import <CommonCrypto/CommonDigest.h>
@implementation NSData (NSData_MD5)
- (NSString *)md5 {
unsigned char hash[CC_MD5_DIGEST_LENGTH];
CC_MD5([self bytes], [self length], hash);
char hashstr[CC_MD5_DIGEST_LENGTH*2];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
snprintf(&hashstr[i*2], 2, "%.2x", hash[i]);
}
return [[[NSString alloc] initWithBytes:hashstr length:CC_MD5_DIGEST_LENGTH*2 encoding:NSASCIIStringEncoding] autorelease];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment