Search code examples
c#cryptographymd5

System.Security.Cryptography.MD5 Hash is producing two different hashes for same file contents created at different times


I have a very simple application that's attempting to compute the MD5 hashes of two different files.

The contents of the files is just the text: abc (I checked for spaces and newlines - they are identical).

I tested two files created at the same minute, and they both return the same hash. However, when testing two files where one was created a couple minutes after the first, they produced different hashes (not sure if the time of creation is the issue here, but in case it's relevant).

Here's the code used to generate the hashes:

var file1 = new FileStream("./file1.txt", FileMode.Open);
var file2 = new FileStream("./file2.txt", FileMode.Open);

using var md51 = System.Security.Cryptography.MD5.Create();
var bytes1 = md51.ComputeHash(file1);
using var md52 = System.Security.Cryptography.MD5.Create();
var bytes2 = md52.ComputeHash(file2);

Console.WriteLine(string.Join(", ", bytes1.Select(b => b.ToString())));
Console.WriteLine(string.Join(", ", bytes2.Select(b => b.ToString())));

However, the process is generating two different hashes:

83, 164, 146, 222, 186, 225, 199, 171, 246, 97, 88, 19, 180, 181, 202, 153
144, 1, 80, 152, 60, 210, 79, 176, 214, 150, 63, 125, 40, 225, 127, 114

Is this how MD5 hashes work? I was under the impression that they only hash the contents of the file itself.

Thanks!


Solution

  • As Damien_The_Unbeliever pointed out, after opening the contents in a hex-editor there were three extra bytes at the beginning of one of the files:

    byte differences in files

    I'm not certain how these bytes were added, but it does explain why the hashes are different.