- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想使用 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);
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
并不真正需要该参数。但它是具有此签名的接口(interface) ICryptoTransform
的实现。所以 SHA1.TransformBlock()
有那个(无用的)参数。请注意,您可以将输出设置为 null
(未记录但有效)。
请注意,在 HashAlgorithm
(即实现 ICryptoTransform
的 SHA1
的基类)中,TransformBlock
有一个line喜欢:
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
因此,如果您将其设置为 null
或 input == output
,则不会复制任何内容。
关于c# - 如何使用HashAlgorithm.TransformBlock/TransformFinalBlock?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50984183/
我正在尝试将充气城堡用于 DTLS 握手。 我按照 this link 生成了 key .我正在通过扩展 DefaultTlsClient 来工作。它可以生成client_hello 数据包。但是当
我无法得到关于这个问题的明确答案,所以这个问题。过去很少有 SO 帖子提到 HashAlgorithm 的实例在 MSDN 文档中不是线程安全的引用片段。 见 Why does SHA1.Comput
从 HashAlgorithm 派生的对象(例如 MD5CryptoServiceProvider)具有 Dispose() 方法,但它是私有(private)的。相反,它有一个 Clear() 方法
我有一个散列算法对象(在本例中为 SHA1),我向其提供数据,以便在调用 Result 属性时最终获得散列结果。 问题是一旦调用了 m_HashAlgorithm.Hash 属性,该对象就不能再用于喂
我需要独立计算多个数据 block 的哈希值。像这样: using( HashAlgorithm hasher = new ActualHashAlgorithm() ) { for( int
使用时 MD5CryptoServiceProvider我发现它可能需要处理,因为它继承自 HashAlgorithm实现 IDisposable 的类.然而,the example in the d
方法 HashAlgorithm.TransformBlock 接受参数 outputBuffer和 outputOffset . 我想不出这个方法有这些的原因。将散列数据复制到某个地方以至于该副本成
我有以下用例: 从一个文件中读取n个字节 计算这 n 个字节的 (MD5) 散列 从文件中读取下m个字节 为最多 n+m 字节的文件计算 (MD5) 哈希 增量散列文件不是问题,just call T
所以我想知道哈希算法的各种实现之间是否存在重大差异,以SHA系列算法为例。它们每个都有 3 个实现,1 个在托管代码中,2 个围绕不同的 native 加密 API 进行包装,但是使用它们之间有什么主
我似乎看到 SHA-2 以两种不同的方式完成。 是的,我知道 1 是“新的”(实例化的),而另一个是 Create 方法,但它似乎实现了相同的相同对象 HashAlgorithm sha2 = new
我需要在 C# 中使用 salt 对密码进行哈希处理。 salt是从一个来源获取的,类型是byte[],password是从另一个来源获取的,类型是String。 计算一个区 block 的哈希值非常
我想了解散列算法的工作原理,特别是 SHA3-512。为了了解它是如何工作的,我在 Google 中搜索了代码并找到了 Hashlib。 .该代码不起作用,因为我没有 Hashlib 库(不确定应该调
这两个代码块返回相同的东西吗?假设 arr 在两个例子中都是相同的 byte[]: 代码示例 1 HashAlgorithm a = HashAlgorithm.Create("SHA-256");
这两个代码块返回相同的东西吗?假设 arr 在两个例子中都是相同的 byte[]: 代码示例 1 HashAlgorithm a = HashAlgorithm.Create("SHA-256");
我正在使用 spymemcached 和 HashAlgorithm.KETAMA_HASH 连接到 5 个节点的 memcached 池。 我的理解是,当我们使用一致的哈希算法时,例如,当节点关闭时
HashAlgorithm.TransformBlock() 具有 outputBuffer 参数,该参数记录为 用于计算哈希码的输入数组部分的副本。 这听起来像是我的数据将被读取,用于更改哈希机制状
我有一个 ASP.Net MVC 项目,它在使用 HashAlgorithm 时运行良好,但我试图在 ASP.NET Core 2 中复制同一个项目,但出现以下错误: System.PlatformN
我是一名优秀的程序员,十分优秀!