hex string to SHA256 digest in python

pycrypto, python, python-3.x

Solution

Use `binascii.unhexlify`:

>>> import binascii
>>> binascii.unhexlify("257612236efae809c23330ab67cf61f73aec938503f3ce126c34c6a32059f5f0")
b'%v\x12#n\xfa\xe8\t\xc230\xabg\xcfa\xf7:\xec\x93\x85\x03\xf3\xce\x12l4\xc6\xa3 Y\xf5\xf0'

Problem

I have a string that contain a SHA256 digest in hexadecimal like blow: ``` "257612236efae809c23330ab67cf61f73aec938503f3ce126c34c6a32059f5f0" ``` and I want to convert it to `hash.digest()` that can be like below: ``` b'%v\x12#n\xfa\xe8\t\xc230\xabg\xcfa\xf7:\xec\x93\x85\x03\xf3\xce\x12l4\xc6\xa3 Y\xf5\xf0' ``` how can I achive this? I use `Crypto.Hash` and `python 3.3.2`

Original source