5

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
alizx
  • 1,148
  • 2
  • 15
  • 27

1 Answers1

5

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'
falsetru
  • 357,413
  • 63
  • 732
  • 636