Python 256bit Hash function with number output

邮差的信 提交于 2019-12-21 03:41:06

问题


I need a Hash function with a 256bit output (as long int).

First I thought I could use SHA256 from the hashlib but it has an String Output and I need a number to calculate with.

Converting the 32 Byte String to a long would work also but I didn't find anything. In struct there is a unpack function but this only works for 8 Byte long types and not for longer longs.


回答1:


How about:

>>> import hashlib
>>> h = hashlib.sha256('something to hash')
>>> h.hexdigest()
'a3899c4070fc75880fa445b6dfa44207cbaf924a450ce7175cd8500e597d3ec1'
>>> n = int(h.hexdigest(),base=16)
>>> print n
73970130776712578303406724846815845410916448611708558169000368019946742824641



回答2:


python 3.x update

import hashlib
value = 'something to hash'
t_value = value.encode('utf8')
h = hashlib.sha256(t_value)
h.hexdigest()
n = int(h.hexdigest(),base=16)
print(n)


来源:https://stackoverflow.com/questions/4612150/python-256bit-hash-function-with-number-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!