ole*_*sii 17
你最好的选择是截断众所周知的哈希函数(MD5或SHA-family),因为这些算法在哈希值上具有统计上良好的均匀分布(并且还使用完整哈希而不仅仅是6个字符).
现在对碰撞概率进行一些计算
- Number of letters in English alphabet: 26 - Add capitals: 26 - Add numerics: 10 -------------- In total you get 26 + 26 + 10 = 62 characters. Now you have 6 places, which gives you 62^6 possible combinations. That is 56.800.235.584 ~ 57 billion combinations. This is a space of possible hash values - N. -------------- To compute collisions let's use the formula Pcollision = K^2 / 2N Which is a very rough approximation of collision probability
现在让我们看一下表格中许多项目的结果表--K
# items | Probability of collision --------------------------------------- 10 | 1.7 * 10^-9 100 | 1.7 * 10^-7 1K | 1.7 * 10^-5 10K | 1.7 * 10^-3 100K | 0.17
此公式只能用于小K,但它表明在哈希表中给定100K条目时,大概有17%的碰撞几率.
轻松哈希:)
private string Hash(string str)
{
var allowedSymbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
var hash = new char[6];
for (int i = 0; i < str.Length; i++)
{
hash[i % 6] = (char)(hash[i % 6] ^ str[i]);
}
for (int i = 0; i < 6; i++)
{
hash[i] = allowedSymbols[hash[i] % allowedSymbols.Length];
}
return new string(hash);
}
Run Code Online (Sandbox Code Playgroud)