Here's the code:
static byte[] hashWithMd5(string text)
{
using (var hasher = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(text);
byte[] hash = hasher.ComputeHash(inputBytes);
return hash;
}
}
Code for SHA256 is the same except that SHA256Managed.Create()
is called.
Each is run one million times to hash the same hard-coded 20-characters string. Time is measured using DateTime.UtcNow. Release
is targeted, code runs outside debugger. Target framework version is 4.5.2.
The results are:
x64
MD5 - 5.89 seconds
SHA256Managed - 2.97 seconds
x86
MD5 - 6.68 seconds
SHA256Managed - 3.57 seconds
So no matter what configuration is run SHA256 turns out notably faster than MD5.
How could this realistically happen? Isn't SHA256 more computationally intensive?