Question

HashAlgorithm.TransformBlock() has outputBuffer parameter which is documented as A copy of the part of the input array used to compute the hash code. which sounds like my data will be read, used to alter the hash mechanism state and also copied to outputBuffer.

I don't need that copying. It looks like I can pass null instead and it looks working.

Should I expect any problems if I pass null as outputBuffer?

Was it helpful?

Solution

Yes, it's fine to pass null. The reason it even has this parameter is because it's implementing the ICryptoTransform interface. This interface can be used when constructing CryptoStreams because you might want to build up a set of transformations. In this case, HashAlgorithm doesn't change the data at all so it ends up funnily defined as just copying the input to the output.

Other implementations of ICryptoTransform (e.g. anything that actually performs encryption or decryption) would of course also be writing non-trivial output.

This means that, during a single pass over an input, you can compute the hash while also performing encryption - that's why this interface is supported here.


The current implementation just has this, after it's done its work:

 if ((outputBuffer != null) && ((inputBuffer != outputBuffer) ||
      (inputOffset != outputOffset)))
      Buffer.BlockCopy(inputBuffer, inputOffset,
           outputBuffer, outputOffset, inputCount);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top