C# 快速哈希计算

发布于 2024-07-25 18:44:30 字数 316 浏览 5 评论 0原文

我正在寻找本机 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

药祭#氼 2024-08-01 18:44:30

MD5 和 SHA1 依赖于本机实现,尽管如此,C++ 解决方案 + introp 可能会稍微快一些,因为您可以稍微减少方法调用的数量并优化本机实现。

请记住,本机 (SHA1CryptoServiceProvider) 的速度比托管 (SHA1Managed) 快 3 倍。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Security.Cryptography;

namespace ConsoleApplication22 {



    class Program {

        static void Profile(string description, int iterations, Action func) {

            // clean up
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            // warm up 
            func();

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++) {
                func();
            }
            watch.Stop();
            Console.Write(description);
            Console.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds);
        }

        static void Main() {
            SHA1Managed managed = new SHA1Managed();
            SHA1CryptoServiceProvider unmanaged = new SHA1CryptoServiceProvider();

            Random rnd = new Random();

            var buffer = new byte[100000];
            rnd.NextBytes(buffer);

            Profile("managed", 1000, () => {
                managed.ComputeHash(buffer, 0, buffer.Length);
            });

            Profile("unmanaged", 1000, () =>
            {
                unmanaged.ComputeHash(buffer, 0, buffer.Length);
            });

            Console.ReadKey();
        }
    }
}
managed Time Elapsed 891 ms
unmanaged Time Elapsed 336 ms

另请记住除非我的计算错误,否则非托管实现会在大约 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).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Security.Cryptography;

namespace ConsoleApplication22 {



    class Program {

        static void Profile(string description, int iterations, Action func) {

            // clean up
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            // warm up 
            func();

            var watch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++) {
                func();
            }
            watch.Stop();
            Console.Write(description);
            Console.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds);
        }

        static void Main() {
            SHA1Managed managed = new SHA1Managed();
            SHA1CryptoServiceProvider unmanaged = new SHA1CryptoServiceProvider();

            Random rnd = new Random();

            var buffer = new byte[100000];
            rnd.NextBytes(buffer);

            Profile("managed", 1000, () => {
                managed.ComputeHash(buffer, 0, buffer.Length);
            });

            Profile("unmanaged", 1000, () =>
            {
                unmanaged.ComputeHash(buffer, 0, buffer.Length);
            });

            Console.ReadKey();
        }
    }
}
managed Time Elapsed 891 ms
unmanaged Time Elapsed 336 ms

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.

命硬 2024-08-01 18:44:30

SHA1CryptoServiceProvider 类使用底层 Windows API 实现。 但是,SHA1Managed 速度相当快。

编辑:是的,可以逐步计算哈希值。 TransformBlockTransformFinalBlock 方法执行此操作。

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 and TransformFinalBlock methods do this.

甜柠檬 2024-08-01 18:44:30

我只想使用 BCL 的 SHA1MD5CryptoServiceProvider 类。 框架附带的速度相当快。

I would just use the BCL's SHA1 and MD5CryptoServiceProvider classes. The ones that ship with the framework are quite fast.

单调的奢华 2024-08-01 18:44:30

根据您的哈希应用程序,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文