When is the number of bytes written by HashAlgorithm.TransformBlock different from the input size?

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

  •  28-05-2021
  •  | 
  •  

質問

The MSDN states about TransformBlock:

public int TransformBlock(
  byte[] inputBuffer,
  int inputOffset,
  int inputCount,
  byte[] outputBuffer,
  int outputOffset
)

Return Value: The number of bytes written.

I'm assuming the output buffer is of sufficient size. Typically, the input buffer is used as output buffer, both offset 0, so this is not an issue.

Is there any condition that could cause the return value to be not equal to the inputCount?

役に立ちましたか?

解決

Assuming the .net implementation is equivalent to the mono implementation, the answer is: never. It throws an exception, or it returns inputCount.

This strange method exists because it implements ICryptoTransform. ICryptoTransform is also used by block ciphers, where a difference between input and output size can indeed happen.

IMO this is bad class design. The interface implementation should have been explicit, and the user should have been given a clean interface, similar to HashCore and HashFinal, which are the extension points of HashAlgorithm.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top