Is the .NET string hash function portable? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-03 05:20:29

From MSDN:

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

So no, you cannot assume that the value produced by GetHashCode is stable. This isn't just theoretical, either - we've seen the value change in the past.

If you want a stable hash, you'll have to generate it yourself.

http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx

the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

No. It is not portable. You should never use this method for anything other than balancing a hash tree. it's implementation has changed between versions of the Framework, and behaves differently for 32-bit / 64-bit CLR.

Eric Lippert has a blog post on rules and proper uses for this function.

Instead, you should be using SHA1Managed for inserting a hash into the database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!