我目前正在使用 MD5 哈希值,但我想找到一些可以创建更短的哈希值的方法,该哈希值仅使用 [a-z][A-Z][0-9]
。它只需要大约 5-10 个字符长。
有什么东西已经这样做了吗?
更新 1:
我喜欢 CRC32 哈希。 .NET 中是否有一种干净的计算方法?
更新 2:
我正在使用 Joe 提供的链接中的 CRC32 函数。如何将 uInt 转换为上面定义的字符?
最佳答案
.NET 字符串对象有一个 GetHashCode() 函数。它返回一个整数。 将其转换为十六进制,然后转换为 8 个字符长的字符串。
像这样:
string hashCode = String.Format("{0:X}", sourceString.GetHashCode());
更多信息:http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx
更新:将上述链接中的备注添加到此答案:
The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. A reason why this might happen is to improve the performance of GetHashCode.
If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.
Notes to Callers
The value returned by GetHashCode is platform-dependent. It differs on the 32-bit and 64-bit versions of the .NET Framework.
关于c# - 创建短哈希的最佳方法是什么,类似于小 URL 所做的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1116860/