Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...