如何使用HashAlgorithm.TransformBlock/TransformFinalBlock?

How to use HashAlgorithm.TransformBlock / TransformFinalBlock?

我想使用 TransformBlock()/TransformFinalBlock() 分几步计算 SHA1 哈希:

byte[] block1 = Encoding.ASCII.GetBytes("This");
byte[] block2 = Encoding.ASCII.GetBytes("is");
byte[] block3 = Encoding.ASCII.GetBytes("Sparta");

SHA1 sha = new SHA1Managed();
sha.TransformBlock(block1, 0, block1.Length, block1, 0);
sha.TransformBlock(block2, 0, block2.Length, block2, 0);
sha.TransformFinalBlock(block3, 0, block3.Length);

byte[] result = sha.Hash;

我知道还有其他方法可以计算 SHA1(例如:HashAlgorithm.ComputeHash()CryptoStream)。上面是更复杂代码的简化版本。

我完全不清楚的是要为 outputBuffer 数组传递什么(TransformBlock 方法的第四个参数) :

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

MSDN page 说:

Computes the hash value for the specified region of the input byte array and copies the specified region of the input byte array to the specified region of the output byte array

如果我不需要那个数组副本怎么办?我应该通过 null 吗? (避免每次都复制输入数组?)

有这个的典型用法吗?

同样,TransformFinalBlock() 似乎也将输入数组复制到输出数组。 AFAIKm 这是方法返回的内容:

byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount);

您链接的页面和示例非常清楚:

Calling the TransformBlock method with different input and output arrays results in an IOException.

连例子都说的很清楚了:

offset += sha.TransformBlock(input, offset, size, input, offset);

SHA1 并不真的需要那个参数。但它是具有此签名的接口 ICryptoTransform 的实现。所以 SHA1.TransformBlock() 有那个(无用的)参数。请注意,您可以将输出设置为 null(未记录但有效)。

请注意,在 HashAlgorithm 中(即实现 ICryptoTransformSHA1 的基础 class),在 TransformBlock 中有一个 line 赞:

if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
    Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);

因此,如果您将其设置为 nullinput == output,则不会复制任何内容。