Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 256bit Hash function with number output

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.

like image 691
Steffen Avatar asked Jan 06 '11 05:01

Steffen


People also ask

How does Python calculate SHA256 hash of a file?

SHA256 can be calculated to text data easily by using the hashlib module sha256() method. The text data is provided as a parameter to the sha356() method. This method generates a result where the hexdigest() method of the result can be used to print the SHA265 in hexadecimal format.


1 Answers

How about:

>>> import hashlib
>>> h = hashlib.sha256('something to hash')
>>> h.hexdigest()
'a3899c4070fc75880fa445b6dfa44207cbaf924a450ce7175cd8500e597d3ec1'
>>> n = int(h.hexdigest(),base=16)
>>> print n
73970130776712578303406724846815845410916448611708558169000368019946742824641
like image 145
DSM Avatar answered Oct 20 '22 01:10

DSM