具有数字输出的Python 256位散列函数 [英] Python 256bit Hash function with number output

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

问题描述



首先,我想我可以使用hashlib中的SHA256,但它有一个字符串输出和我需要一个数字来计算。

将32字节字符串转换为长字符也行,但我没有找到任何东西。
在struct中有一个解压缩函数,但是这只适用于8字节长的类型,而不适用于更长的时间。

解决方案

如何:

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


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.

解决方案

How about:

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

这篇关于具有数字输出的Python 256位散列函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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