Calculating an MD5 hash is a fairly simple task using the CommonCrypto library, which I mentioned previously in an article on getting Gravatars which involved calculating the MD5 hash of a user’s e-mail.
While it is easy to do these calculations with CommonCrypto it is extremely inefficient to do so with the commonly used high level NSFileHandle commands. Unfortunately, the problem with this solution is that it makes very inefficient use of memory because NSFileHandle returns an autorelease object making it impractical for large files. One solution to this is to use the CFReadStream.
With this solution you can keep the memory usage low while reading in very large files.
You can read a more in depth tutorial on this topic here:
Compute MD5 Or SHA Hash Efficiently On iOS
A Github project has also been created to make this solution more straightforward:
If you’re in a situation where you need to calculate the MD5 hash of a file, and are running into memory problems this should help.
Thanks for reading, please share this using the buttons below!
Submit A Resource
Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?
You can submit the url here.
The resources we feel will appeal to our readers the most will be posted on the front page.