Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a SHA256 hash from integer to string and back?

Tags:

python

I would like to store a very large number of SHA256 bit hashes in MySQL. I do not want to store them as VARCHAR(64), but rather as BIGINT. What is the most efficient way to do the conversions in Python? Illustration of the code I want to accomplish:

>>> import hashlib
>>> s = hashlib.sha256("foo").hexdigest()
>>> s
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
>>> i = int(s, 16)
>>> i
19970150736239713706088444570146546354146685096673408908105596072151101138862L

Given i, how do I get back s exactly? I have tried:

>>> hex(i)
'0x2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7aeL'

This is close but not the same. Thanks!

EDIT: I would like to avoid transforming the hex string like to get rid of the leading 0x and trailing L:

>>> hex(i)[2:-1]
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'

Also if the hash has leading zero bits I'd have to pad zeroes until it's length 64 like:

>>> s = "00000000000000003258ec44ea34c98111234904ef7642608922f9b8b296067d"
>>> i = int(s, 16)
>>> i
1234513556969586830351532907052865231487419381722987759229L
>>> h = hex(i)[2:-1]
>>> h
'3258ec44ea34c98111234904ef7642608922f9b8b296067d'
>>> h = "0" * (64 - len(h)) + h
'00000000000000003258ec44ea34c98111234904ef7642608922f9b8b296067d'

I'd like to avoid the string manipulation.

like image 218
Ben Wilber Avatar asked Feb 13 '23 10:02

Ben Wilber


1 Answers

If you don't like the string representation hex gives you, you can use string formatting instead:

'{:x}'.format(i)
Out[32]: '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'

edit: string manipulation allows you to specify a minimum width and a fill value, too:

'{:0>64x}'.format(i)
Out[54]: '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'

s = "00000000000000003258ec44ea34c98111234904ef7642608922f9b8b296067d"

i = int(s,16)

'{:0>64x}'.format(i)
Out[57]: '00000000000000003258ec44ea34c98111234904ef7642608922f9b8b296067d'

Or if you'd like, just use zfill on the first method:

'{:x}'.format(i).zfill(64)
Out[67]: '00000000000000003258ec44ea34c98111234904ef7642608922f9b8b296067d'
like image 76
roippi Avatar answered Feb 16 '23 03:02

roippi