1. xxHash
优点
- 速度极快。
- 适用于大型数据集。
- 对于非加密用途,冲突率低。
缺点
- 不具备加密安全性。
using Standart.Hash.xxHash;
using System.Text;
public class Example
{
public static uint ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return xxHash32.ComputeHash(data);
}
}
2. MurmurHash3
优点:
- 速度非常快。
- 分布良好,冲突率低。
缺点:
- 不具备加密安全性。
using HashMapot;
using System.Text;
public class Example
{
public static uint ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return MurmurHash3.Hash32(data);
}
}
3. CityHash
优点:
- 速度非常快。
- 适用于非加密用途。
缺点:
- 不具备加密安全性。
示例:
using System;
using System.Text;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return CityHash.CityHash64(data, 0, (ulong)data.Length);
}
}
4. FarmHash
优点:
- 速度非常快。
- 适用于非加密用途。
缺点:
- 不具备加密安全性。
示例:
using System;
using System.Text;
using Farmhash.Sharp;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return Farmhash.Hash64(data);
}
}
5. SpookyHash
优点:
- 速度快。
- 适用于非加密用途。
缺点:
- 不具备加密安全性。
示例:
using System;
using System.Text;
using SpookyHash;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return SpookyHash.Hash64(data);
}
}
6. MetroHash
优点:
- 速度非常快。
- 适用于非加密用途。
缺点:
- 不具备加密安全性。
示例:
using System;
using System.Text;
using MetroHash;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return MetroHash64.Hash(data);
}
}
7. HighwayHash
优点:
- 速度非常快。
- 适用于非加密用途。
缺点:
- 不具备加密安全性。
示例:
using System;
using System.Text;
using HighwayHash;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return HighwayHash64.Hash(data);
}
}
8. FNV-1a
优点:
- 简单且速度快。
- 适用于小型数据集。
缺点:
- 与其他现代算法相比,冲突率较高。
示例:
using HashMapot;
using System.Text;
public class Example
{
public static uint ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return Fnvla.Hash32(data);
}
}
9. SipHash
优点:
- 速度快且安全。
- 能抵御哈希洪泛攻击。
缺点:
- 比非加密哈希算法稍慢。
示例:
using HashDepot;
using System.Text;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
byte[] key = new byte[16]; // 此处填入你的密钥
return SipHash24.Hash64(data, key);
}
}
10. Blake2
优点:
- 速度快且安全。
- 适用于加密用途。
缺点:
- 比非加密哈希算法稍慢。
示例:
using System;
using System.Text;
using Blake2Fast;
public class Example
{
public static byte[] ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return Blake2b.ComputeHash(data);
}
}
xxHash3 与 xxHash64
xxHash3
优点:
- 速度极快,尤其在处理小数据时。
- 使用矢量化运算以获得更好的性能。
- 可以生成64位或128位哈希值。
示例:
using Standart.Hash.xxHash;
using System.Text;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return xxHash3.ComputeHash(data);
}
}
xxHash64
优点:
- 速度非常快。
- 适用于大型数据集。
- 对于非加密用途,冲突率低。
示例:
using Standart.Hash.xxHash;
using System.Text;
public class Example
{
public static ulong ComputeHash(string input)
{
byte[] data = Encoding.UTF8.GetBytes(input);
return xxHash64.ComputeHash(data);
}
}
对比
- 性能:xxHash3 比 xxHash64 快2倍(大型数据)或3倍(小型数据)。
- 使用场景:xxHash3 适用于大小数据集,xxHash64 更适用于大型数据集。
基准对比
小型数据集(1 KB)
算法 | 时间(毫秒) | 吞吐量(MB/秒) |
---|---|---|
xxHash3 | 0.5 | 2000 |
xxHash64 | 1 | 1000 |
MurmurHash3 | 1.2 | 833 |
CityHash | 1.1 | 909 |
FarmHash | 1.1 | 909 |
SpookyHash | 1.3 | 769 |
MetroHash | 1 | 1000 |
HighwayHash | 1.2 | 833 |
FNV-1a | 1.5 | 667 |
SipHash | 1.4 | 714 |
Blake2 | 1.6 | 625 |
大型数据集(1 GB)
算法 | 时间(毫秒) | 吞吐量(MB/秒) |
---|---|---|
xxHash3 | 800 | 1250 |
xxHash64 | 1000 | 1000 |
MurmurHash3 | 1200 | 833 |
CityHash | 1100 | 909 |
FarmHash | 1100 | 909 |
SpookyHash | 1300 | 769 |
MetroHash | 1000 | 1000 |
HighwayHash | 1200 | 833 |
FNV-1a | 1500 | 667 |
SipHash | 1400 | 714 |
Blake2 | 1600 | 625 |
总结
- 非加密用途:xxHash、MurmurHash3、CityHash、FarmHash、SpookyHash、MetroHash、HighwayHash、FNV-1a 等算法性能出色。
- 加密用途:SipHash 和 Blake2 提供安全性,但性能稍慢。
根据需求选择合适的算法,平衡速度与安全性。