For a current project I must generate a large number of MD5 hashes. I explicitly don’t want to discuss the meaning and security of MD5 hashes in this article, but rather concern myself with the question on how to generate MD5 hashes as fast as possible using C#.
The. NET Framework itself provides a class for creating MD5 hashes, which after the first attempts seemed a little slow to me. So I began to search for alternative classes and/or functions and came across a class of Syed Faraz Mahmood. His class is a manual implementation of the RFC 1321 (“The MD5 Message-Digest Algorithm”). The class can be downloaded for free on his blog.
The test environment
For the test I created six lists (List<int>) with many different numbers. Each with 1,000, 10,000, 50,000, 100,000, 1,000,000 and 10,000,000 elements.
All lists were run complete for both methods (MD5 .NET framework implementation & manual MD5 implementation) and each element of each list was hashed. Here, the time for each list runs was measured and plotted in a graph in order to evaluate the results.
You can view the source code of my test environment on github or directly download the Visual Studio project here from my blog.
Benchmark – the results
Like it is apparent from the graphs, that the manual implementation is round about 2/3 faster than the implementation of. NET framework.
Also it can be noted that this advantage in speed is constant. Thus, it remains at the 1/3 of time required, even for large lists. A slump in performance when changing the list size was not observed.
For my part I’ll fall back for future project on the implementation of Syed Faraz Mahmood. Thanks to Syed for his great piece of code!
Have you also already involved in this topic or even already implemented an algorithm for generating MD5 hashes? Do you take my test for representative or do you have suggestions? Feedback is always welcome!