来自c#中可变长度字符串的固定长度数字哈希码

时间:2009-02-13 23:57:57

标签: c# .net-3.5 hash

我需要存储由可变长度字符串生成的固定长度(最多8位)数字。哈希不必是唯一的。它只需要在输入字符串更改时更改。 .Net中是否有哈希函数来执行此操作?

感谢
基肖尔马布。

3 个答案:

答案 0 :(得分:22)

我假设你这样做是因为你需要将值存储在别处并与之进行比较。因此,Zach的答案(虽然完全正确)可能会引起您的问题,因为String.GetHashCode()的合同明确了它的更改范围。

因此,这是一个固定的,易于重复的其他语言版本。

我假设您在编译时会知道可用的小数位数。 这是基于Jenkins One At a Hash(由Bret Mulvey实现和exhaustively tested),因此它具有出色的雪崩行为(输入中的一位变化传播到输出的所有位)意味着在最后使用稍微懒惰的模数减少并不是大多数用途的严重缺陷(尽管你可以用更复杂的行为做得更好)

const int MUST_BE_LESS_THAN = 100000000; // 8 decimal digits

public int GetStableHash(string s)
{
    uint hash = 0;
    // if you care this can be done much faster with unsafe 
    // using fixed char* reinterpreted as a byte*
    foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
    {   
        hash += b;
        hash += (hash << 10);
        hash ^= (hash >> 6);    
    }
    // final avalanche
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    // helpfully we only want positive integer < MUST_BE_LESS_THAN
    // so simple truncate cast is ok if not perfect
    return (int)(hash % MUST_BE_LESS_THAN)
}

答案 1 :(得分:6)

简单方法(请注意,这是platform-dependent):

int shorthash = "test".GetHashCode() % 100000000; // 8 zeros
if (shorthash < 0) shorthash *= -1;

答案 2 :(得分:0)

使用System.Security.Cryptography.MD5CryptoServiceProvider.ComputeHash获取MD5哈希值,将其截断为所需的长度。

相关问题