.NET框架提供了6种不同的哈希算法:
- MD5:16字节(哈希500MB的时间:1462毫秒)
- SHA-1:20字节(1644毫秒)
- SHA256:32字节(5618毫秒)
- SHA384:48字节(3839毫秒)
- SHA512:64字节(3820毫秒)
- RIPEMD:20字节(7066毫秒)
每个函数的性能不同;MD5是最快的,而RIPEMD是最慢的。
MD5的优点是它适合内置的Guid类型;并且它是类型3 UUID的基础。SHA-1哈希是类型5 UUID的基础。这使得它们非常易于用于标识。
然而,MD5容易受到碰撞攻击的影响,SHA-1也容易受到影响,但程度较轻。
在什么条件下应该使用哪种哈希算法?
我真正想知道的问题是:
MD5不可信吗?在没有恶意意图且没有第三方有任何恶意意图的情况下,您是否预期会出现任何碰撞(即两个任意byte[]产生相同的哈希值)
RIPEMD比SHA1好多少?(如果有的话)计算时间慢了5倍,但哈希大小与SHA1相同。
在对文件名(或其他短字符串)进行哈希时,获得非恶意碰撞的几率有多大? (例如,具有相同MD5哈希的2个随机文件名)(使用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);
}
foreach (var pair in algorithms) {
TimeAction(pair.Key + " calculation", 500, () =>
{
pair.Value.ComputeHash(source);
});
}
Console.ReadKey();
}