Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hex string to SHA256 digest in python

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

like image 745
alizx Avatar asked Nov 20 '13 12:11

alizx


People also ask

Is SHA256 output hexadecimal?

SHA256 Results The SHA256 online generator allows you to instantly generate a SHA256 (32-byte) hash of any string or input value, which is then returned as a hexadecimal number of 64 digits.

How do you convert hex to string in Python?

hex() function in Python. hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.

What is the hex digest of a hash?

From the documentation for hash. hexdigest() : the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.


1 Answers

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'
like image 131
falsetru Avatar answered Sep 23 '22 16:09

falsetru