Tuesday, May 3, 2011

MD5 in Objective C

Just Import  CommonCrypto/CommonDigest.h and following method returns md5 string.

 #import <CommonCrypto/CommonDigest.h>

- (NSString *)convertIntoMD5:(NSString *) string{
    const char *cStr = [string UTF8String];
    unsigned char digest[16];

    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
   
    NSMutableString *resultString = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
   
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [resultString appendFormat:@"%02x", digest[i]];
    return  resultString;
}

No comments:

Post a Comment