PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. //
  2. // NSString+MD5.m
  3. //
  4. // Created by Jack Lawrence on 8/16/11.
  5. // Source: http://iphonedevelopertips.com/core-services/create-md5-hash-from-nsstring-nsdata-or-file.html
  6. //
  7. #import "NSString+MD5.h"
  8. #import <CommonCrypto/CommonDigest.h>
  9. @implementation NSString(MD5)
  10. - (NSString*)MD5
  11. {
  12. // Create pointer to the string as UTF8
  13. const char *ptr = [self UTF8String];
  14. // Create byte array of unsigned chars
  15. unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
  16. // Create 16 byte MD5 hash value, store in buffer
  17. CC_MD5(ptr, strlen(ptr), md5Buffer);
  18. // Convert MD5 value in the buffer to NSString of hex values
  19. NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  20. for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
  21. [output appendFormat:@"%02x",md5Buffer[i]];
  22. return output;
  23. }
  24. @end