如何强制执行.NET字符串GetHash code 32位()字符串、强制执行、NET、GetHash

2023-09-06 06:15:44 作者:知男而上

可能重复:   GetHash$c$c()给不同的服务器上不同的结果?

我使用的字符串GetHash code()在我的.NET 4.0程序并存储在数据库中的值。问题是,当我运行在不同的安装方案,64和32,.NET中使用不同的哈希算法的字符串散列。所以,我不会得到比赛的所有时间。因此,没有人知道我可以只计算字符串即使是在64位版本散列32位。

I used string GetHashCode() in my .net 4.0 program and store the value in the DB. The problem is that when I run the program on different setup, 64 vs 32, .net use different hashing algorithm for string hashing. So I won’t get the match all the time. So does anyone know how can I just compute the 32bit of string hashing even on a 64bit build.

推荐答案

简单的答案就是那样做。该值被指定为平台相关的,以及确切的行为是不符合的GetHash code合同之外定义。它不能保证留跨越.NET的新版本,新的架构,甚至补丁一样。用一个实际的哈希算法(视实际需求),其行为定义,如SHA-1,CRC,还是其他什么东西。

The simple answer is to not do that. The values are specified as platform-dependent, and the exact behavior is not defined outside of satisfying the contract of GetHashCode. It's not guaranteed to stay the same across new releases of .NET, new architectures, or even patches. Use an actual hash algorithm whose behavior is defined, such as SHA-1, CRC, or something else (depending on the actual requirements).