Here is how to create an MD5 (Message-Digest Algorithm) of NSString in xcode
Create Interface with fileName as NSString+MD5
@interface NSString(MD5)
- (NSString *)MD5;
@end
Implementing the interface
#import <CommonCrypto/CommonDigest.h>
@implementation NSString(MD5)
- (NSString*)MD5
{
// Creating the pointer to the string as UTF8String
const char *ptr = [self UTF8String];
// Creating byte array of unsigned characters
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
// Creating 16 byte MD5 hash value and storing it in buffer
CC_MD5(ptr, strlen(ptr), md5Buffer);
// Converting MD5 value from 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]];
//Returning the string hex values
return output;
}
@end
How to call the MD5 method
Import NSString+MD5.h into the place where you want to create MD5
Now call the MD5 method using the NSString object as follows
NSString *toConvertString = @”the string to convert to MD5″;
NSLog(@”Converted str -> %@”, [toConvertString MD5]);