Posted By

phifty on 08/11/09


Tagged

unique objc hash id md5


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

cnsaturn
mracoker


MD5 String Hash


 / Published in: Objective C
 

This is a simple md5 implementation.

  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
Posted By: gravityone on May 9, 2010

Thanks for the snippet it was very helpful, although its a bit verbose. I made a post on my blog showing similar code and how to use it. You can see that here, http://www.saobart.com/md5-has-in-objective-c/

You need to login to post a comment.