简体   繁体   English

我可以将null作为HashAlgorithm.TransformBlock()的“输出缓冲区”参数传递吗?

[英]Can I just pass null as “output buffer” parameter of HashAlgorithm.TransformBlock()?

HashAlgorithm.TransformBlock() has outputBuffer parameter which is documented as A copy of the part of the input array used to compute the hash code. HashAlgorithm.TransformBlock()具有outputBuffer参数,该参数记录为用于计算哈希码的输入数组部分的副本。 which sounds like my data will be read, used to alter the hash mechanism state and also copied to outputBuffer . 这听起来像我的数据将被读取,用于改变哈希机制状态,也复制到outputBuffer

I don't need that copying. 我不需要复制。 It looks like I can pass null instead and it looks working. 看起来我可以传递null ,它看起来很有效。

Should I expect any problems if I pass null as outputBuffer ? 如果我将null作为outputBuffer传递,我应该期待任何问题吗?

Yes, it's fine to pass null . 是的,传递null是可以的。 The reason it even has this parameter is because it's implementing the ICryptoTransform interface. 它甚至有这个参数的原因是因为它正在实现ICryptoTransform接口。 This interface can be used when constructing CryptoStream s because you might want to build up a set of transformations. 构造CryptoStream时可以使用此接口,因为您可能希望构建一组转换。 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. 在这种情况下, HashAlgorithm根本不会更改数据,所以它最终被定义为只是将输入复制到输出。

Other implementations of ICryptoTransform (eg anything that actually performs encryption or decryption) would of course also be writing non-trivial output. ICryptoTransform其他实现(例如,实际执行加密或解密的任何实现)当然也会写出非平凡的输出。

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM