散列函数产生短散列? [英] Hash function that produces short hashes?
问题描述
尽管如果任意长度的字符串是不可能的,我可以将消息限制为整数值。但是,对于两个连续的整数,哈希不能相似。
您可以使用任何常用的哈希算法(例如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屋!