MD5 hash on iPhone with cocoa and Objective-C
In beta 7 OpenSSL has been removed from the iPhone SDK. However, MD5 is still available.
Simply import CommonCrypto as follows:
#import < CommonCrypto/CommonDigest.h >
Then add this C function to your objective-c class between the @implementation and @end statements (if you like).
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]
];
}
I am sure you can just use NSData for this but this is the way an example was posted on apple forums. Please feel free to add an NSData based solution.
Read the post here "http://discussions.apple.com/thread.jspa?threadID=1509152&tstart=96"
Simply import CommonCrypto as follows:
#import < CommonCrypto/CommonDigest.h >
Then add this C function to your objective-c class between the @implementation and @end statements (if you like).
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]
];
}
I am sure you can just use NSData for this but this is the way an example was posted on apple forums. Please feel free to add an NSData based solution.
Read the post here "http://discussions.apple.com/thread.jspa?threadID=1509152&tstart=96"
Labels: iPhone



6 Comments:
thanks ....... it works beautifully ! but I think it produces output in capital letters whereas it should give output in small letters!!
thanks ....... it works beautifully ! but I think it produces output in capital letters whereas it should give output in small letters!!
thanks, works perfectly!
The md5 output of my string of letters was an uppercase hash. If I run md5 on the same string in php for example, the string is the same except for the case. Not sure which one is right though.
My site has a free MD5 Hash generation tool, might be usefull sometime?
Well, if you're trying to crack an MD5 for some reason or another you can try out http://www.netmd5crack.com
It's been proven that there are better hashing algorithms available. I think it's to we move away from md5.
Post a Comment
Links to this post:
Create a Link
<< Home