2

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?

sharptooth
  • 167,383
  • 100
  • 513
  • 979

1 Answers1

0

Multiple reasons why this might happen. While md5 is generally faster than sha2 you can check this: https://security.stackexchange.com/questions/95696/which-hash-algorithm-takes-longer-time-if-we-compare-between-md5-or-sha256

So it depends on various factors:

  1. Hardware acceleration, based on your machine. If sha256 uses the GPU, you'll see much faster results.
  2. Implementation details (as mentioned in the comment). On unix it is generally faster with md5 as expected.

Check this paper here: https://www.google.com/url?sa=t&source=web&rct=j&url=https://iopscience.iop.org/article/10.1088/1742-6596/978/1/012116/pdf&ved=2ahUKEwjx99e8_-jtAhVGzRoKHXM_DE4QFjAMegQICRAB&usg=AOvVaw1VVgJ-fEQddNca2P4OQi8p Your results, seem to confirm the Θ(n) mentioned, but not the slightly higher results.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61