MD5 Hashes in Cocoa « Tom Dalling.

MD5 Hashes in Cocoa

Let’s jump straight into the code:

Now for the explanation.

First, the input string is encoded into UTF8 data so that the hash comes out the same as an ASCII string hash, unless there are non-ASCII characters in the string.

Next, the CC_MD5 function creates the 16 byte MD5 hash. CC_MD5 is a function from CommonCrypto, which is part of the Foundation framework for Mac and iPhone. Because you will already be using the Foundation framework, you don’t need to link against any additional libraries or frameworks.

Finally, the 16 byte hash is converted to a 32 character hexadecimal string; the most commonly used format.

Here is how the function is used:

You may even want to wrap the hashes in a hash class.

A word of caution: if the hash is generated from sensitive information such as passwords, then you should be aware of hashes weakness to precomputation attacks. Make sure you sprinkle some salt first.

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注