I am following this link, trying to perform SHA-256 on a given public key.
However, I cannot get the expected result.
I am using the sha256 library in python hashlib

The input is


Mine output is


The expected output on bitcoin.it is


Do I miss anything?

1 Answer 1


The result you are getting (3251...) is the result of sha256 on the string 0450....

The expected output on bitcoin.it is the result of sha256 on the bytes that are currently hex-encoded. To resolve this, hex-decode before hashing your input. In Python 2:

import hashlib
in_data = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6".decode('hex')
my_sha = hashlib.sha256()
output = my_sha.digest().encode('hex')

That should retain output with the expected value.

  • 1
    Yep, that's tripped me up too. You'll need to use bytes objects - b"hexData" - for Python 3.x Apr 8, 2015 at 15:43
  • 1
    Good point. I'm not sure how to write the code so it's compatible with Python 3 ("string".encode('hex') is replaced), so unless you do I'll just specify Python 2 here.
    – JohnDvorak
    Apr 8, 2015 at 15:49
  • 1
    import codecs, then codecs.encode(codecs.decode("abcdef".encode("utf-8"), "hex")[::-1], "hex").decode()... It's quite counterintuitive IMO, as I'm so used to Python 2.7 Apr 8, 2015 at 15:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.