Created
October 25, 2013 23:35
-
-
Save priore/7163432 to your computer and use it in GitHub Desktop.
MD5 encode NSString
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
// MD5 | |
#import <CommonCrypto/CommonDigest.h> | |
- (NSString*)getMD5WithString: (NSString*)str{ | |
// Create pointer to the string as UTF8 | |
const char *ptr = [str UTF8String]; | |
// Create byte array of unsigned chars | |
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH]; | |
// Create 16 byte MD5 hash value, store in buffer | |
CC_MD5(ptr, strlen(ptr), md5Buffer); | |
// Convert MD5 value in the buffer to NSString of hex values | |
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; | |
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) | |
[output appendFormat:@"%02x",md5Buffer[i]]; | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment