15

The method HashAlgorithm.TransformBlock takes the arguments outputBuffer and outputOffset.

I can't think of a reason for this method to have those. Is it so common to copy the hashed data somewhere that the copy was made an integral part of this method?

I'm rather curious to see a good use case involving these output parameters.

3
  • 2
    Additionally, the documentation doesn't mention that outputBuffer may actually be set to null to avoid the copy, and insists that "calling the TransformBlock method with different input and output arrays results in an IOException", which is not true. Commented Jan 28, 2011 at 19:26
  • 2
    API design at its best! So common in .net though... Commented Jan 28, 2011 at 19:35
  • 1
    Yeah, after looking through Reflector, I have no idea. Commented Jan 28, 2011 at 20:35

1 Answer 1

7

It's pretty clearly an oversight. I couldn't find a source to back this up, but as it stands any use case we could imagine (I can't imagine any practical use cases) saves developers at most one line of code. Worse, any developer who (for whatever reason) did want a copy of his own buffer made would probably find it so unlikely that TransformBlock would do this for her that she'd probably write that line of code herself anyway!

outputBuffer is used by other ICryptoTransforms in sensible ways (eg. as an output buffer), so it really should contain - when not null and not overwriting the input buffer - the intermediate digest, especially since it's not otherwise available from the class. (Making the intermediate digest available is a nice idea whenever the input to the algorithm appears in multiple contexts.)

My guess is:

  1. Someone got it backwards in 1.1 regarding when the IOException should be thrown.
  2. In 2.0, someone "fixed" that problem, but misread the spec and copied from inputBuffer instead of this.HashValue.
  3. Intermediate digests are used so rarely that no one has complained before 4.0.
Sign up to request clarification or add additional context in comments.

3 Comments

Oooh, right. It's because this implements an ICryptoTransform method, which was meant for things that produce outputs. I should have noticed that...
To make things worse, method summary in my IDE says, it is actually Hash which is written to output array. See this: i.imgur.com/W16YB.png
After reading this question and a blog post that shows using hashes with CryptoStream, it seems the intent was probably to make HashAlgorithm some kind of tee-like transform, that doesn't modify the data that goes through it but intercepts a copy of it for its hash.

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.