Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Possible Duplicate:
MD5 algorithm in Objective C

I need to hash a string using the MD5 technique in cocoa. Any frameworks that are used must be able to be accessed on the iphone. please provide code if possible.

like image 627
zpesk Avatar asked Mar 16 '09 21:03

zpesk


People also ask

How do I check if a string is an MD5?

You can check using the following function: function isValidMd5($md5 ='') { return preg_match('/^[a-f0-9]{32}$/', $md5); } echo isValidMd5('5d41402abc4b2a76b9719d911017c592'); The MD5 (Message-digest algorithm) Hash is typically expressed in text format as a 32 digit hexadecimal number.

What is MD5 string?

What is MD5? MD5 (message-digest algorithm) is a cryptographic protocol used for authenticating messages as well as content verification and digital signatures. MD5 is based on a hash function that verifies that a file you sent matches the file received by the person you sent it to.

How can the MD5 value of a file be useful?

MD5 is an older cryptographic hash function that is no longer considered secure for many applications. It turns data of any length into a fixed-length output. This output has a range of useful properties. These properties make MD5 safe for data identification and for verifying whether data has been corrupted.

How do I use MD5 code?

Type the following command: md5sum [type file name with extension here] [path of the file] -- NOTE: You can also drag the file to the terminal window instead of typing the full path. Hit the Enter key. You'll see the MD5 sum of the file. Match it against the original value.


3 Answers

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;
}
...
like image 178
Jackie Treehorn Avatar answered Oct 17 '22 02:10

Jackie Treehorn


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)

like image 36
Chad Birch Avatar answered Oct 17 '22 03:10

Chad Birch


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

like image 19
bentford Avatar answered Oct 17 '22 02:10

bentford