创建一个简短哈希的最佳方式是什么,类似于小型Url所做的? [英] What's the best way to create a short hash, similar to what tiny Url does?

查看:106
本文介绍了创建一个简短哈希的最佳方式是什么,类似于小型Url所做的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用MD5哈希值,但我想找到一些能够创建一个只使用[a-z] [A-Z] [0-9]的更短的哈希值。它只需要大约5-10个字符。



有没有那样的东西已经做到了?

更新: 我喜欢CRC32散列。有没有一种干净的方式计算它在.NET中?



Update2:

我使用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



更新:在上面的链接中添加了以下评论:


GetHashCode的行为取决于其实现,
可能会从公共语言运行时的一个版本更改为另一个版本的
。这可能发生的一个原因是提高了GetHashCode的性能


如果两个字符串对象相等,则GetHashCode方法返回
个相同的值。但是,对于
每个唯一的字符串值,没有唯一的哈希码值。 不同的字符串可以返回相同的散列码
代码。

$ b 来电者注释



GetHashCode返回的值是平台相关的。它不同于
的.NET Framework的32位和64位版本。



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?

Update:

I like the CRC32 hash. Is there a clean way of calculating it in .NET?

Update2:

I'm using the CRC32 function from the link Joe provided. How can I convert the uInt into the characters defined above?

解决方案

.NET string object has a GetHashCode() function. It returns an integer. Convert it into a hex and then to an 8 characters long string.

Like so:

string hashCode = String.Format("{0:X}", sourceString.GetHashCode());

More on that: http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx

UPDATE: Added the remarks from the link above to this answer:

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.

这篇关于创建一个简短哈希的最佳方式是什么,类似于小型Url所做的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆