散列函数产生短散列? [英] Hash function that produces short hashes?

查看:155
本文介绍了散列函数产生短散列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种单向加密可以采取任何长度的字符串并产生一个10分之10字符的哈希值?我想生成合理的唯一ID,但是基于消息内容,而不是随机。



尽管如果任意长度的字符串是不可能的,我可以将消息限制为整数值。但是,对于两个连续的整数,哈希不能相似。

解决方案

您可以使用任何常用的哈希算法(例如SHA-1),这将比您需要的结果稍长一些。简单地将结果截断到所需的长度,这可能足够好。



例如,在Python中:

 >>> import hashlib 
>>> hash = hashlib.sha1(我的消息.encode(UTF-8))。hexdigest()
>>>哈希
'104ab42f1193c336aa2cf08a2c946d5c6fd0fcdb'
>>>哈希[:10]
'104ab42f11'


IS there a one way encryption that can take a string of any length and produce a sub-10-character hash? I want to produce reasonably unique ID's but based on message contents, rather than randomly.

I can live with constraining the messages to integer values, though, if arbitrary-length strings are impossible. However, the hash must not be similar for two consecutive integers, in that case.

解决方案

You can use any commonly available hash algorithm (eg. SHA-1), which will give you a slightly longer result than what you need. Simply truncate the result to the desired length, which may be good enough.

For example, in Python:

>>> import hashlib
>>> hash = hashlib.sha1("my message".encode("UTF-8")).hexdigest()
>>> hash
'104ab42f1193c336aa2cf08a2c946d5c6fd0fcdb'
>>> hash[:10]
'104ab42f11'

这篇关于散列函数产生短散列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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