Last active
December 18, 2015 21:48
-
-
Save claybridges/5849642 to your computer and use it in GitHub Desktop.
MD5StringFromBytes
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 <CommonCrypto/CommonDigest.h> | |
NSString *MD5StringFromBytes(uint8_t *bytes, NSUInteger count) | |
{ | |
uint8_t md5Bytes[CC_MD5_DIGEST_LENGTH]; | |
CC_MD5(bytes, count, md5Bytes); | |
// convert to hex string | |
static char hexLookup[] = "0123456789abcdef"; | |
char hexCString[CC_MD5_DIGEST_LENGTH * 2 + 1]; // each byte converts to two hex characters, plus null terminator | |
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; ++i) { | |
uint8_t fullByte = md5Bytes[i]; | |
hexCString[2 * i] = hexLookup[fullByte >> 4]; // high-order nibble first | |
hexCString[2 * i + 1] = hexLookup[fullByte & 0x0F]; // low-order | |
} | |
hexCString[CC_MD5_DIGEST_LENGTH * 2] = 0; // null terminate | |
return [NSString stringWithUTF8String:hexCString]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment