What is the best solution in C# for computing an “on the fly” md5 like hash of a stream of unknown length? Specifically, I want to compute a hash from data received over the network. I know I am done receiving data when the sender terminates the connection, so I don’t know the length in advance.
[EDIT] – Right now I am using md5 and am doing a second pass over the data after it’s been saved and written to disk. I’d rather hash it in place as it comes in from the network.
MD5, like other hash functions, does not require two passes.
To start:
As each block of data arrives:
To finish and retrieve the hash:
This pattern works for any type derived from
HashAlgorithm, includingMD5CryptoServiceProviderandSHA1Managed.HashAlgorithmalso defines a methodComputeHashwhich takes aStreamobject; however, this method will block the thread until the stream is consumed. Using theTransformBlockapproach allows an “asynchronous hash” that is computed as data arrives without using up a thread.