Skip to content

Instantly share code, notes, and snippets.

@claybridges
Last active December 18, 2015 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claybridges/5849642 to your computer and use it in GitHub Desktop.
Save claybridges/5849642 to your computer and use it in GitHub Desktop.
MD5StringFromBytes
#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