.NET框架附帶6種不同的哈希算法:MD5:16個字節(散列時間500MB:1462毫秒)SHA-1:20個字節(1644毫秒)SHA256:32個字節(5618毫秒)SHA384:48個字節(3839毫秒)SHA512:64個字節(3820毫秒)RIPEMD:20個字節(7066 ms)這些功能中的每個功能都有不同。MD5是最快的,而RIPEMD是最慢的。MD5的優點是適合內置的Guid類型。它是類型3 UUID的基礎。SHA-1哈希是類型5 UUID的基礎。這使得它們非常易于識別。但是,MD5容易受到碰撞攻擊,SHA-1也容易受到攻擊,但是程度較小。在什么情況下應該使用哪種哈希算法?我真的很想知道答案的具體問題是:MD5不值得信賴嗎?在正常情況下,當您使用沒有惡意意圖的MD5算法并且任何第三方都沒有惡意意圖時,您會期望發生任何沖突(這意味著兩個任意byte []會產生相同的哈希)RIPEMD比SHA1好多少?(如果更好),其計算速度要慢5倍,但哈希大小與SHA1相同。在對文件名(或其他短字符串)進行哈希處理時,獲得非惡意沖突的幾率是多少?(例如,兩個具有相同MD5哈希值的隨機文件名)(帶有MD5 / SHA1 / SHA2xx)通常,非惡意沖突的幾率是多少?這是我使用的基準: static void TimeAction(string description, int iterations, Action func) { var watch = new Stopwatch(); watch.Start(); for (int i = 0; i < iterations; i++) { func(); } watch.Stop(); Console.Write(description); Console.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds); } static byte[] GetRandomBytes(int count) { var bytes = new byte[count]; (new Random()).NextBytes(bytes); return bytes; } static void Main(string[] args) { var md5 = new MD5CryptoServiceProvider(); var sha1 = new SHA1CryptoServiceProvider(); var sha256 = new SHA256CryptoServiceProvider(); var sha384 = new SHA384CryptoServiceProvider(); var sha512 = new SHA512CryptoServiceProvider(); var ripemd160 = new RIPEMD160Managed(); var source = GetRandomBytes(1000 * 1024); var algorithms = new Dictionary<string,HashAlgorithm>(); algorithms["md5"] = md5; algorithms["sha1"] = sha1; algorithms["sha256"] = sha256; algorithms["sha384"] = sha384; algorithms["sha512"] = sha512; algorithms["ripemd160"] = ripemd160; foreach (var pair in algorithms) { Console.WriteLine("Hash Length for {0} is {1}", pair.Key, pair.Value.ComputeHash(source).Length); }
我應該選擇哪個加密哈希函數? 問問題
慕田峪4524236
2019-11-22 13:07:04