MD5 hash of blob uploaded on Azure doesnt match with same file on local machine

15,809

Here is a good article on how to calculate and check Blob MD5 checksums.

I have faced this before, and I don't know why, but you can'T just do md5.computeHash(fileBytes). For Azure Blobs, it uses the following path to get the hash:

// Validate MD5 Value
var md5Check = System.Security.Cryptography.MD5.Create();
md5Check.TransformBlock(retrievedBuffer, 0, retrievedBuffer.Length, null, 0);     
md5Check.TransformFinalBlock(new byte[0], 0, 0);

// Get Hash Value
byte[] hashBytes = md5Check.Hash;
string hashVal = Convert.ToBase64String(hashBytes);

and it works...

And yes, as Guarav already mentioned - MD5 hash is saved as base64 string.

Share:
15,809

Related videos on Youtube

Salman
Author by

Salman

By Day: A techie, programmer working on multiple products and solutions By Night: Web crawler, catching up on new stuff coming in

Updated on September 16, 2022

Comments

  • Salman
    Salman over 1 year

    I am currently working on uploading media on Azure Blob storage. All is working fine except when i try to macth the MD5 hash of uploaded media with the local file (exactly same one which was uploaded). Local file returns a byte array where are blob.Properties.ContentMD5 returns a string and both do not match.

    Local MD5 hash: sÔ(F¦‚"“Db~[N

    blob.Properties.ContentMD5: c9QoHkamgiKTRANifltOGQ==

    Any possible way to match both these?

    • Gaurav Mantri
      Gaurav Mantri almost 9 years
      Try to convert MD5 byte array into Base64 string and see if that matches.
  • Salman
    Salman almost 9 years
    Thanks. Your code returns the same string as blob.Properties.ContentMD5. However, i used the same code to convert the byte array from source file to Base64String and it works!
  • Manfred
    Manfred about 7 years
    Code example for a shorter version using the newer MD5.ComputeHash(Stream stream) can be found at stackoverflow.com/a/43647643/411428
  • Myles
    Myles over 2 years
    It looks like that article is no longer available on the blog. Here is an archive copy from when this answer was posted.