如何为 64 位应用程序检索 String.GetHashcode() 的源代码?
How to retrieve source code of String.GetHashcode() for 64 bits application?
我犯了一个错误,将 String.GetHashCode()
用于稳定,因此我通过组合 32 位 String.GetHashCode()
构建了一些 "unique" 128 位哈希。而且,确实在 MSDN 文档中,我不应该将此方法用于稳定,因为我在 32 位应用程序上没有相同的哈希值。
我无法回滚,因为我所有的数据都是用这个错误写入的。
在这一点上不要对我大吼大叫。
现在,我需要在 64 位平台中恢复 GetHashCode() 的实际实现,以稳定我的代码。
有没有地方可以找到它?
这个 64 位实现应该 return(我使用 .NET 4.7.0)
"a".GetHashCode() == 372029373; // Should be true
对于那些犯同样错误的人,我已经根据评论员的文档构建了这个实现。它是以下特性的默认实现:
- .NET 4 - 4.7.2
- 64 位
- 发布模式
public static class StringHashExtensions
{
public static unsafe int GetHashCode64BitsRelease(this string str)
{
unsafe
{
fixed (char* src = str)
{
int hash1 = 5381;
int hash2 = hash1;
int c;
char* s = src;
while ((c = s[0]) != 0)
{
hash1 = ((hash1 << 5) + hash1) ^ c;
c = s[1];
if (c == 0)
break;
hash2 = ((hash2 << 5) + hash2) ^ c;
s += 2;
}
return hash1 + (hash2 * 1566083941);
}
}
}
}
我犯了一个错误,将 String.GetHashCode()
用于稳定,因此我通过组合 32 位 String.GetHashCode()
构建了一些 "unique" 128 位哈希。而且,确实在 MSDN 文档中,我不应该将此方法用于稳定,因为我在 32 位应用程序上没有相同的哈希值。
我无法回滚,因为我所有的数据都是用这个错误写入的。
在这一点上不要对我大吼大叫。
现在,我需要在 64 位平台中恢复 GetHashCode() 的实际实现,以稳定我的代码。
有没有地方可以找到它?
这个 64 位实现应该 return(我使用 .NET 4.7.0)
"a".GetHashCode() == 372029373; // Should be true
对于那些犯同样错误的人,我已经根据评论员的文档构建了这个实现。它是以下特性的默认实现:
- .NET 4 - 4.7.2
- 64 位
- 发布模式
public static class StringHashExtensions
{
public static unsafe int GetHashCode64BitsRelease(this string str)
{
unsafe
{
fixed (char* src = str)
{
int hash1 = 5381;
int hash2 = hash1;
int c;
char* s = src;
while ((c = s[0]) != 0)
{
hash1 = ((hash1 << 5) + hash1) ^ c;
c = s[1];
if (c == 0)
break;
hash2 = ((hash2 << 5) + hash2) ^ c;
s += 2;
}
return hash1 + (hash2 * 1566083941);
}
}
}
}