MD5, like other hash functions, does not require two passes.
To start:
HashAlgorithm hasher = ..;
hasher.Initialize();
As each block of data arrives:
byte[] buffer = ..;
int bytesReceived = ..;
hasher.TransformBlock(buffer, 0, bytesReceived, null, 0);
To finish and retrieve the hash:
hasher.TransformFinalBlock(new byte[0], 0, 0);
byte[] hash = hasher.Hash;
This pattern works for any type derived from HashAlgorithm
, including MD5CryptoServiceProvider
and SHA1Managed
.
HashAlgorithm
also defines a method ComputeHash
which takes a Stream
object; however, this method will block the thread until the stream is consumed. Using the TransformBlock
approach allows an "asynchronous hash" that is computed as data arrives without using up a thread.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…