I want to compute an SHA1 hash in several steps using TransformBlock()/TransformFinalBlock() :
byte[] block1 = Encoding.ASCII.GetBytes("This");
byte[] block2 = Encoding.ASCII.GetBytes("is");
byte[] block3 = Encoding.ASCII.GetBytes("Sparta");
SHA1 sha = new SHA1Managed();
sha.TransformBlock(block1, 0, block1.Length, block1, 0);
sha.TransformBlock(block2, 0, block2.Length, block2, 0);
sha.TransformFinalBlock(block3, 0, block3.Length);
byte[] result = sha.Hash;
I know there is other ways to compute SHA1 (eg : HashAlgorithm.ComputeHash() or CryptoStream). What is above is a simplified version of more complex code.
What is totally unclear to me is what to pass for the outputBuffer array (the fourth parameter of TransformBlock method) :
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount,
byte[] outputBuffer, int outputOffset);
The MSDN page says :
Computes the hash value for the specified region of the input byte array and copies the specified region of the input byte array to the specified region of the output byte array
What if I don't need that array copy ? Should I pass null ? (to avoid input array to be copied each time ?)
Is there a typical use of this ?
Similarly, it seems TransformFinalBlock() also copied input array to an output array. AFAIKm this is what is returned by the method :
byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount);
"This"is not a block. However, instead of offering a low level interface it offers a high level interface that doesn't do what you would expect, confusing the casual reader. I'd rather use the streaming interface instead. The original designer should hide in shame for this interface definition.