MD5 String Hash


/ Published in: Objective C
Save to your folder(s)

This is a simple md5 implementation.


Copy this code and paste it in your HTML
  1. #import <CommonCrypto/CommonDigest.h>
  2. #import <Foundation/Foundation.h>
  3.  
  4. @interface Util : NSObject {}
  5. // generates a unique id from a string
  6. + (NSString *)uniqueIDFromString:(NSString *)source;
  7. @end
  8.  
  9. @implementation Util
  10. // generates a unique id from a string
  11. + (NSString *)uniqueIDFromString:(NSString *)source
  12. {
  13. const char *src = [[source lowercaseString] UTF8String];
  14. unsigned char result[CC_MD5_DIGEST_LENGTH];
  15. CC_MD5(src, strlen(src), result);
  16.  
  17. NSString *ret = [[[NSString alloc] initWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  18. result[0], result[1], result[2], result[3],
  19. result[4], result[5], result[6], result[7],
  20. result[8], result[9], result[10], result[11],
  21. result[12], result[13], result[14], result[15]
  22. ] autorelease];
  23.  
  24. return ret;
  25. }
  26. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.