Advertisement
priore

MD5 encode NSString

Aug 30th, 2013
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // MD5
  2. #import <CommonCrypto/CommonDigest.h>
  3. - (NSString*)getMD5WithString: (NSString*)str{
  4.     // Create pointer to the string as UTF8
  5.     const char *ptr = [str UTF8String];
  6.    
  7.     // Create byte array of unsigned chars
  8.     unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
  9.    
  10.     // Create 16 byte MD5 hash value, store in buffer
  11.     CC_MD5(ptr, strlen(ptr), md5Buffer);
  12.    
  13.     // Convert MD5 value in the buffer to NSString of hex values
  14.     NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  15.     for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
  16.         [output appendFormat:@"%02x",md5Buffer[i]];
  17.    
  18.     return output;
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement