0

I'm trying to get SHA256 output for hexadecimal values but only able to give string input. The Python 3 code I use is given below

from hashlib import sha256
hash = sha256(b'0x01')
hx=hash.hexdigest()
print('hx: ', hx)

What I get

hx:  b51c80e50d664f281fedd07ba902a21caadc0c7e0b491ece679497391e9f84cf

What I expect

hx:  4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a

The expected output is from the online tool SHA256 Online with input type as hex.

1 Answer 1

2

Convert the hexadecimal string to bytes using bytes.fromhex():

from hashlib import sha256
hash = sha256(bytes.fromhex('01'))
hx = hash.hexdigest()
print('hx: ', hx)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.