C# 快速哈希计算
我正在寻找本机 MD5 或 SHA1 库的 ac# 包装器,以提高哈希计算性能。
之前我将 SharpZipLib 切换为 zlib,并获得了超过 2 倍的性能提升。 (好吧,你必须注意你有正确的 zlib.so 或 zlib.dll,具体取决于操作系统和硬件,但它是有回报的)。
MD5 或 SHA1 是否值得,或者 .NET 和 Mono 都依赖本机实现?
(已编辑)另外:如果我必须坚持使用 MD5CryptoServiceProvider,有没有一种方法可以在读取文件时计算文件的哈希值? 我的意思是,以块的形式发送字节但仍然计算整个哈希值?
I'm looking for a c# wrapper to a native MD5 or SHA1 library to improve hash calculation performance.
Previously I switched SharpZipLib to zlib and got more than 2x performance boost. (ok, you've to take care you've the right zlib.so or zlib.dll depending on the OS and hardware, but it pays off).
Will it be worth for MD5 or SHA1 or both .NET and Mono rely on a native implementation already?
(Edited) Also: in case I've to stick to the MD5CryptoServiceProvider, is there a way in which I can calculate a hash of a file while I'm reading it? I mean, send bytes in chunks but still calculate the whole hash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MD5 和 SHA1 依赖于本机实现,尽管如此,C++ 解决方案 + introp 可能会稍微快一些,因为您可以稍微减少方法调用的数量并优化本机实现。
请记住,本机 (SHA1CryptoServiceProvider) 的速度比托管 (SHA1Managed) 快 3 倍。
另请记住除非我的计算错误,否则非托管实现会在大约 300 毫秒内对 100MB 数据进行哈希处理,这很少会成为瓶颈。
MD5 and SHA1 rely on native implementaions, nonetheless its possible a C++ solution + introp could be slightly faster, cause you could possibly reduce the number of method calls a bit and optimize the native implementation.
Keep in mind that the Native (SHA1CryptoServiceProvider) can be 3X faster than the managed one(SHA1Managed).
Also Keep in mind unless my calculation is wrong, the unmanaged implementation is hashing 100MB of data in about 300 milliseconds, this would very rarely be a bottleneck.
SHA1CryptoServiceProvider
类使用底层 Windows API 实现。 但是,SHA1Managed
速度相当快。编辑:是的,可以逐步计算哈希值。
TransformBlock
和TransformFinalBlock
方法执行此操作。The
SHA1CryptoServiceProvider
class uses the underlying Windows API implementation. However,SHA1Managed
is pretty fast.EDIT: Yes, it's possible to compute the hash step by step. The
TransformBlock
andTransformFinalBlock
methods do this.我只想使用 BCL 的 SHA1 和MD5CryptoServiceProvider 类。 框架附带的速度相当快。
I would just use the BCL's SHA1 and MD5CryptoServiceProvider classes. The ones that ship with the framework are quite fast.
根据您的哈希应用程序,MD5 可能不适用。 MD5 仅在纠错方面有用,它不再可用于检查恶意文件更改。
http://en.wikipedia.org/wiki/Md5#Vulnerability
短篇故事也就是说,更改文件中的 16 个字节很容易产生 MD5 冲突。
Depending on your application of hashing, MD5 might not be applicable. MD5 is only useful in error correction, it's no longer viable as a check against malicious file alteration.
http://en.wikipedia.org/wiki/Md5#Vulnerability
The short story is, MD5 collisions are easy to generate by changing 16 bytes in a file.