Can I just pass null as "output buffer" parameter of HashAlgorithm.TransformBlock()?

StackOverflow https://stackoverflow.com/questions/22954940

  •  30-06-2023
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top