CBeltz
CBeltz

Reputation: 47

hashlib.sha256 yields unexpected result

I'm trying to implement the following steps in Python:

Compressed public key:
02F840A04114081690223B7069071A70D6DABB891763B638CC20C7EC3BD58E6C86

SHA-256 of public key yields:
cb05d0fd5e76ba8ea88323fc5d3eefd09a78d8e2a5fd4955307b549657a31330

This step is fairly simple, so I wrote this piece of code to see if it works as planned:

from binascii import hexlify
from hashlib import sha256
master_key = hexlify("02F840A04114081690223B7069071A70D6DABB891763B638CC20C7EC3BD58E6C86")
print(sha256(master_key).hexdigest())

However, this doesn't yield the expected result. Instead it gives me:

cee4b5650664b11623675d0371ab9dd1e3478758a95b189e54ecf8b7bdd7ba2d

Using the string without hex-encoding it first doesn't yield the correct result either. Any ideas?

Upvotes: 0

Views: 84

Answers (1)

hiro protagonist
hiro protagonist

Reputation: 46921

you want to unhexlify your hexadecimal representation:

from binascii import unhexlify
from hashlib import sha256
master_key = unhexlify("02F840A04114081690223B7069071A70D6DABB891763B638CC20C7EC3BD58E6C86")
print(sha256(master_key).hexdigest())
# cb05d0fd5e76ba8ea88323fc5d3eefd09a78d8e2a5fd4955307b549657a31330

Upvotes: 2

Related Questions