Jeff
Jeff

Reputation: 7210

Python sha256 hex string and binary string treated differently

I have a hex and I'm getting the binary representation by doing the following

In [10]: binascii.unhexlify('950ef5cce9c32676ab67998e2245f682624f9abce6bb1392b67204159b82c020')
Out[10]: '\x95\x0e\xf5\xcc\xe9\xc3&v\xabg\x99\x8e"E\xf6\x82bO\x9a\xbc\xe6\xbb\x13\x92\xb6r\x04\x15\x9b\x82\xc0 '

Just checking my own sanity I can get back the original value

In [11]: binascii.hexlify('\x95\x0e\xf5\xcc\xe9\xc3&v\xabg\x99\x8e"E\xf6\x82bO\x9a\xbc\xe6\xbb\x13\x92\xb6r\x04\x15\x9b\x82\xc0 ')
Out[11]: '950ef5cce9c32676ab67998e2245f682624f9abce6bb1392b67204159b82c020'

Now I want the sha256 of this binary value

In [12]: hashlib.sha256('\x95\x0e\xf5\xcc\xe9\xc3&v\xabg\x99\x8e"E\xf6\x82bO\x9a\xbc\xe6\xbb\x13\x92\xb6r\x04\x15\x9b\x82\xc0 ').hexdigest()
Out[12]: '74d0ef097a15e1699b2476de3b700e86f54d8d3ec65485382c01dc7984b46f96'

I would expect the sha256 of the hex value, that I originally had, would match the binary sha256. It doesn't.

In [13]: hashlib.sha256('950ef5cce9c32676ab67998e2245f682624f9abce6bb1392b67204159b82c020').hexdigest()
Out[13]: 'cb0c91c3977b3433ce481511103e6454a5b781ec57849187aa0b2fe1b20e8078'

My guess is that sha256 is taking the values as literal string representations and from a string standpoint they're different, but from the hex and binary standpoint they're the same.

Is there a way of telling sha256 to tread values it receives as a binary value or do I have to do that explicitly? I didn't see anything in the docs. Just wondering if there is or if someone knows a way of doing this.

Upvotes: 0

Views: 1572

Answers (1)

Heikki Toivonen
Heikki Toivonen

Reputation: 31150

hashlib.sha256 operates on the exact input string. Since the hex string and binary string are different, sha256 of them is different. When you call sha256 you need to ensure you pass the same form consistently (both hex and binary works, but you have to stick to one form consistently).

Upvotes: 2

Related Questions