Using MD5 hash on a string in cocoa? [duplicate]

Noticed this in the Facebook Connect source code. Looks pretty solid, give it a shot.

#import <CommonCrypto/CommonDigest.h>

...

+ (NSString*)md5HexDigest:(NSString*)input {
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, strlen(str), result);

    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}
...

Well, first off, MD5 isn't encryption. So if you're looking for encryption, you're looking in the wrong place.

But if you just want to hash something using MD5 on an iPhone, this should give you the information you need:

#import <CommonCrypto/CommonDigest.h>

NSString *md5(NSString *str) {
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), result );
    return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
        result[0], result[1],
        result[2], result[3],
        result[4], result[5],
        result[6], result[7],
        result[8], result[9],
        result[10], result[11],
        result[12], result[13],
        result[14], result[15]
    ];
}

//…

NSString *digest = md5(@"test");
NSLog(@"MD5 TEST %@", digest);

(From Calculate MD5 on iPhone)


This is what I use. Credits go to Alistair McMillan.

#import <CommonCrypto/CommonDigest.h>


+ (NSString *) md5:(NSString *)str {
 const char *cStr = [str UTF8String];
 unsigned char result[16];
 CC_MD5( cStr, strlen(cStr), result );
 return [NSString stringWithFormat:
  @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  result[0], result[1], result[2], result[3], 
  result[4], result[5], result[6], result[7],
  result[8], result[9], result[10], result[11],
  result[12], result[13], result[14], result[15]
  ]; 
}

NOTE #1: I didn't have to link to any libraries

NOTE #2: I couldn't find -lcrypto in the external framework list on the iphone, and this works without -lcrypto


It's worth mentioning that the OpenSSL methods are deprecated on more recent versions of OS X, and the MD5 digest is conventionally lower case. Personally I'm more a fan of the unrolled style for efficiency, and I think using ObjC categories for this is a better fit.

For MD5Digest.h: #include

@interface NSString (MD5Digest)
- (NSString*) md5Digest;
@end

@interface NSData (MD5Digest)
- (NSString*) md5Digest;
@end

And MD5Digest.m:

#include <CommonCrypto/CommonDigest.h>
#include "MD5Digest.h"

static NSString* md5Digest(const void *data, CC_LONG length)
{
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    unsigned char* d = CC_MD5(data, length, digest);

    return [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
        d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15],
        nil];
}

@implementation NSString (MD5Digest)

- (NSString*) md5Digest
{
    return md5Digest([self UTF8String], [self lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
}

@end

@implementation NSData (MD5Digest)

- (NSString*) md5Digest
{
    return md5Digest([self bytes], [self length]);
}

@end