创建短哈希的最佳方法是什么,类似于 tiny Url 的作用? [英] What's the best way to create a short hash, similar to what tiny Url does?
问题描述
我目前正在使用 MD5 散列,但我想找到一些可以创建仅使用 [a-z][A-Z][0-9]
的更短散列的东西.它只需要大约 5-10 个字符长.
I'm currently using MD5 hashes but I would like to find something that will create a shorter hash that uses just [a-z][A-Z][0-9]
. It only needs to be around 5-10 characters long.
有没有已经做到这一点的东西?
Is there something out there that already does this?
我喜欢 CRC32 哈希.在 .NET 中是否有一种干净的计算方法?
I like the CRC32 hash. Is there a clean way of calculating it in .NET?
我正在使用 Joe 提供的链接中的 CRC32 函数.如何将 uInt 转换为上面定义的字符?
I'm using the CRC32 function from the link Joe provided. How can I convert the uInt into the characters defined above?
推荐答案
.NET 字符串对象有一个 GetHashCode() 函数.它返回一个整数.将其转换为十六进制,然后转换为 8 个字符长的字符串.
.NET string object has a GetHashCode() function. It returns an integer. Convert it into a hex and then to an 8 characters long string.
像这样:
string hashCode = String.Format("{0:X}", sourceString.GetHashCode());
更多相关信息:http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx
更新:将以上链接中的评论添加到此答案中:
UPDATE: Added the remarks from the link above to this answer:
GetHashCode 的行为依赖于它的实现,即可能会从公共语言运行时的一个版本更改为其他.可能发生这种情况的一个原因是为了提高性能的 GetHashCode.
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.
如果两个字符串对象相等,则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.
来电者须知
GetHashCode 返回的值平台相关.它的不同之处在于.NET Framework 的 32 位和 64 位版本.
The value returned by GetHashCode is platform-dependent. It differs on the 32-bit and 64-bit versions of the .NET Framework.
这篇关于创建短哈希的最佳方法是什么,类似于 tiny Url 的作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!