在 C# 中对大文件(超过 2GB)进行哈希 SHA1
我正在寻找散列大文件内容的解决方案(32 位操作系统中的文件可能超过 2GB)。有什么简单的解决办法吗?或者只是部分读取并加载到缓冲区?
I`m looking for solution for hashing large file content (files may be over 2gb in 32bit os). It there any easy solution for that? Or just reading by part and loading to buffer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Driis 的解决方案听起来更灵活,但
HashAlgorithm.ComputeHash
也将接受Stream
作为参数。Driis's solution sounds more flexible, but
HashAlgorithm.ComputeHash
will also acceptStream
s as parameters.使用
TransformBlock
< /a> 和TransformFinalBlock
逐块计算哈希值,因此您不需要将整个文件读入内存。 (第一个链接中有一个很好的例子 - 以及上一个问题中的另一个)。
Use
TransformBlock
andTransformFinalBlock
to calculate the hash block by block, so you won't need to read the entire file into memory. (There is a nice example in the first link - and another one in this previous question).如果您选择使用
TransformBlock
,那么您可以安全地忽略最后一个参数并将outputBuffer设置为null
。 TransformBlock 将从输入复制到输出数组 - 但为什么您会无缘无故地简单复制位呢?此外,所有 mscorlib HashAlgorithms 都按照您的预期工作,即块大小似乎不会影响哈希输出;无论您是在一个数组中传递数据,然后通过更改 inputOffset 进行散列,还是通过传递较小的单独数组进行散列,都无关紧要。我使用以下代码验证了这一点:
(这有点长,只是在这里,以便人们可以自己验证
HashAlgorithm
实现是否正常)。If you choose to use
TransformBlock
, then you can safely ignore the last parameter and set the outputBuffer tonull
. TransformBlock will copy from the input to the output array - but why would you want to simply copy bits for no good reason?Furthermore, all mscorlib HashAlgorithms work as you might expect, i.e. the block size doesn't seem to affect the hash output; and whether you pass the data in one array and then hash in chunks by changing the
inputOffset
or you hash by passing smaller, separate arrays doesn't matter. I verified this using the following code:(this is slightly long, just here so people can verify for themselves that
HashAlgorithm
implementations are sane).