2 回答

TA贡献1815条经验 获得超6个赞
十六进制哈希码可以解释为(相当大的)数字:
import hashlib
hex_hash = hashlib.sha1('hello world'.encode('utf-8')).hexdigest()
int_hash = int(hex_hash, 16) # convert hexadecimal to integer
print(hex_hash)
print(int_hash)
产出
'2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'
243667368468580896692010249115860146898325751533
编辑:如评论中所问,要将数字限制在某个范围内,您可以简单地使用模数运算符。当然,请注意,这会增加发生冲突的可能性。例如,我们可以将“哈希”限制为 0 .. 9,999,999,模数为 10,000,000。
limited_hex_hash = hex_hash % 10_000_000
print(limited_hex_hash)
产出
5751533

TA贡献2080条经验 获得超4个赞
我认为不需要图书馆。您可以使用hash()python 中的函数简单地完成此操作。
InputString="Hello World!!"
HashValue=hash(InputString)
print(HashValue)
print(type(HashValue))
输出:
8831022758553168752
<class 'int'>
基于最新编辑的问题解决方案:
上述方法是最简单的解决方案,更改每次调用的哈希值将帮助我们防止攻击者篡改我们的应用程序。
如果您想关闭随机化,您可以简单地通过分配 PYTHONHASHSEED to zero.
有关关闭随机化检查官方文档https://docs.python.org/3.3/using/cmdline.html#cmdoption-R 的信息
添加回答
举报