Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MD5 file hash for the same unchanged file is different each time C#

Tags:

hash

md5

Good evening all,

I've been working on an MD5 tool in C# that takes a file, goes through my Hasher class and pops the result in a database, along with the filename and directory.

The issue I'm having is that each time I run the test, the MD5 result for the same identical file i.e. unchanged in any way is completely different.

Below is the code I use

HashAlgorithm hmacMd5 = new HMACMD5(); 
byte[] hash;
try
{
    using (Stream fileStream = new FileStream(fileLocation, FileMode.Open))
    {
        using (Stream bufferedStream = new BufferedStream(fileStream, 5600000))
        {
            hash = hmacMd5.ComputeHash(bufferedStream);
            foreach (byte x in hash)
            {
                md5Result += x;
            }
        }
    }
}
catch (UnauthorizedAccessException uae) { }

return md5Result;

Here are the results for 3 seperate runs of hello.mp2:

1401401571161052548110297623915056204169177

16724366215610475211823021169211793421

56154777074212779619017828183239971

Quite puzzling. My only rational thought as to why I'm getting these results is with concatenating byte to string.

Can anyone spot an issue here?

Regards,

Ric

like image 744
Ric Avatar asked Aug 28 '09 15:08

Ric


People also ask

Why are the hashes of the same file different?

So, a Word file and the PDF file published from the Word file may contain the same content, but the HASH value will be different. Even copying the content from one file to another in the same software program can result in different HASH values, or even different file sizes.

What causes MD5 hash to change?

Overview. MD5 Checksum is used to verify the integrity of files, as virtually any change to a file will cause its MD5 hash to change. Most commonly, md5sum is used to verify that a file has not changed as a result of a faulty file transfer, a disk error or non-malicious modification.

Does MD5 hash change?

The MD5 hash will change if there's any change to whatever data you input to the MD5 hashing function. If you fed it the permissions and the permission change, then the MD5 hash will change. If you fed it only the contents, then the MD5 hash will change only if the contents change.

Can different files have the same MD5 hash?

Generally, two files can have the same md5 hash only if their contents are exactly the same. Even a single bit of variation will generate a completely different hash value. There is one caveat, though: An md5 sum is 128 bits (16 bytes).


1 Answers

You should use System.Security.Cryptography.MD5 rather.

HMACMD5 doesn't compute a hash, it computes a message authentication code.

HMACMD5 is a type of keyed hash algorithm that is constructed from the MD5 hash function and used as a Hash-based Message Authentication Code (HMAC). The HMAC process mixes a secret key with the message data, hashes the result with the hash function, mixes that hash value with the secret key again, then applies the hash function a second time. The output hash will be 128 bits in length

Since you're not supplying the HMAC key, one is being generated randomly for you on your behalf and causing you to see different results.

like image 174
nos Avatar answered Sep 28 '22 18:09

nos