I want to store a very large number of SHA256 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 conversions in Python? Illustration of the code I want to execute:
>>> import hashlib
>>> s = hashlib.sha256("foo").hexdigest()
>>> s
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
>>> i = int(s, 16)
>>> i
19970150736239713706088444570146546354146685096673408908105596072151101138862L
Given that I, how can I get back exactly? I tried:
>>> hex(i)
'0x2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7aeL'
It is close, but not the same. Thank!
EDIT: I would like to avoid converting the hex string to get rid of the leading 0x and trailing L:
>>> hex(i)[2:-1]
'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae'
Also, if the hash has leading zero bits, I will need to put zeros until it is 64:
>>> s = "00000000000000003258ec44ea34c98111234904ef7642608922f9b8b296067d"
>>> i = int(s, 16)
>>> i
1234513556969586830351532907052865231487419381722987759229L
>>> h = hex(i)[2:-1]
>>> h
'3258ec44ea34c98111234904ef7642608922f9b8b296067d'
>>> h = "0" * (64 - len(h)) + h
'00000000000000003258ec44ea34c98111234904ef7642608922f9b8b296067d'
I would like to avoid string manipulation.
source
share