0

How to compute MD5 hash for file in C# in next way:

Compute hash for range 1-4096 bytes -> get MD5 hash for this range;
1-8192 -> get MD5 hash for this range;
1-12288 -> get MD5 hash for this range;
1-16384 -> get MD5 hash for this range;
...
1-end of file -> get MD5 hash for file.

So I always read file using buffer with size 4096 bytes and want to update hash for all read part of file. How to realize it? Looks like MD5CryptoServiceProvider can't help with this task.

Dark Hydra
  • 265
  • 6
  • 21
  • 1
    It's not clear why you want to do this. Why not just hash the whole file? Why do you need the hash for each part? – Jon Skeet Jan 18 '14 at 18:52
  • try this link http://stackoverflow.com/questions/827527/c-sharp-md5-hasher-example – Mzf Jan 18 '14 at 18:54
  • I want to compare big count of files by content and reading this files by partial is good strategy to compare them. Checksum of last read part is not what i need, i want to get checksum of all read file part. – Dark Hydra Jan 18 '14 at 19:00

1 Answers1

1

Normally, MD5 and other hashes don't work like that. They use a certain padding in the last block. So if you compute the final hash for some bytes, you can't add more bytes to it later. Once you added the final block and computed the hash, it's game over, you have to start again.

I understand you would like to calculate the hash but also keep going further.

The best strategy is to use a custom MD5 implementation that let's you clone its state. When you add one block, duplicate the state of the MD5 algorithm, so you have to MD5 hashers with the same state. You use one of the clones to finish the transformation. You use the other one to go further. Pseudo-code:

hasher = new md5
loop
    read a block of the file
    hasher.addblock(current block)
    hasher2 = hasher.clone()
    hasher.finish()
    hasher = hasher2
end loop
fejesjoco
  • 11,763
  • 3
  • 35
  • 65
  • Yes, it is what i need. Copy state of hash object. But there are no methods for doing this. I tried code from this answer: http://stackoverflow.com/a/11308879/2183994 but it doesn't work. – Dark Hydra Jan 18 '14 at 19:12
  • The MD5CryptoServiceProvider uses unmanaged API's (the native Windows CSP), so you can't clone that in .NET. You need a pure .NET MD5 implementation, it should be easy to find. There may have been an MD5Managed class in older .NET frameworks, I don't remember exactly, but you can still find classes named like that eg. here: http://archive.msdn.microsoft.com/SilverlightMD5/Wiki/View.aspx?title=MD5Managed – fejesjoco Jan 18 '14 at 19:23