3

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?

1
  • The documentation certainly only talks about ArgumentNullException if the input buffer is okay - but doesn't explicitly talk about what happens with a null output buffer. Hmm. Commented Apr 9, 2014 at 6:56

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.