/NSString+MD5.m
http://github.com/Jlawr3nc3/Objective-C-Category-Collection · Objective C · 33 lines · 14 code · 9 blank · 10 comment · 1 complexity · b5a66f5b24aabb7e23450462568fc885 MD5 · raw file
- //
- // NSString+MD5.m
- //
- // Created by Jack Lawrence on 8/16/11.
- // Source: http://iphonedevelopertips.com/core-services/create-md5-hash-from-nsstring-nsdata-or-file.html
- //
- #import "NSString+MD5.h"
- #import <CommonCrypto/CommonDigest.h>
- @implementation NSString(MD5)
- - (NSString*)MD5
- {
- // Create pointer to the string as UTF8
- const char *ptr = [self 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;
- }
- @end