HashAlgorithm.ComputeHash() 是有状态的吗?
我需要独立计算多个数据块的哈希值。像这样的事情:
using( HashAlgorithm hasher = new ActualHashAlgorithm() ) {
for( int i = 0; i = numberOfBlocks; i++ ) {
byte[] block = getBlock( i );
byte[] hash = hasher.ComputeHash( block );
// use hash
}
}
我可以在块之间重用相同的 HashAlgorithm
对象吗? HashAlgorithm
是否会在调用 ComputeHash()
之间重置状态,或者我是否需要处置 HashAlgorithm
对象并为每个新数据块创建新的对象?
I need to compute hashes of multiple blocks of data independently. Something like this:
using( HashAlgorithm hasher = new ActualHashAlgorithm() ) {
for( int i = 0; i = numberOfBlocks; i++ ) {
byte[] block = getBlock( i );
byte[] hash = hasher.ComputeHash( block );
// use hash
}
}
Can I reuse the same HashAlgorithm
object between blocks? Will HashAlgorithm
reset state between calls to ComputeHash()
or do I need to dispose the HashAlgorithm
object and create new one for each new block of data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用ComputeHash通常是无状态的,尽管它取决于实际的实现...您可以在运行时通过调用ComputeHash后访问State来检查这一点...
请参阅
Using
ComputeHash
is usually stateless although it depends on the actual implementation... you can check that at runtime by accessingState
after the call toComputeHash
...see
实际上,当你在.NET框架下需要哈希时,我强烈建议手动编写这个函数,而不是使用.NET框架。
几个月后,我将 32 位 .NET 程序迁移到 64 位 Windows 中。程序崩溃了。至少我发现虽然相同的.NET程序,但在不同的32/64位系统下哈希值是不同的。
我使用Djb算法代替.NET哈希算法,程序运行正常。
本文档是关于Djb哈希算法的,您可以用C#重写。这不是一项艰苦的工作。
Actually, when you need hash under .NET framework, I strongly recommended coding this function manually but not to use .NET framework.
Some months age, I immigrated a 32bit .NET program into 64bit windows. The program is crashed. At least I found hash value is different under different 32/64bit system, although same .NET program.
I used Djb algorithm instead of .NET hash algorithm, and the program run okay.
This document is about Djb hash algorithm, you can rewrite by C#. It is not a hard work.