Search code examples
pythonsha256ripemd

PKHash RipeMD160 in python


I am trying to understand how the crypto algorithms RIPEMD and SHA256 work. The bitcoin method for computing PKHash is RIPEMD160(SHA256(PublicKey)).

I am trying to first implement the RIPEMD of SHA256(PublicKey).

pkHashStep1=hashlib.sha256(public_key.decode('hex')).digest()
print 'MyTransaction pkHashStep1 {}'.format(pkHashStep1)

MyTransaction pkHashStep1 ▒▒▒so▒/▒▒e▒▒▒¡▒7▒?9▒▒.▒ӟ!n▒h

This outputs a string that I cannot use directly, but the hashlib library can use this. Trying pkHashStep1.decode('hex') and bin(pkHashStep1) throws an error. How does it convert the hash to a usable hexstring/bin??

Currently, I have publicKey as input to my RipeMD method, instead of the pkHashStep1, and have to separately do

input=hashlib.sha256(publicKey.decode('hex')).hexdigest()

FYI: I know there is a ripemd method in hashlib. Suggesting I use it is not an answer https://stackoverflow.com/a/2124289/4219479


Solution

  • glibdud's comment got me to the answer.

    hashlib.sha256(public_key.decode('hex')).digest().encode('hex')=
    hashlib.sha256(public_key.decode('hex')).hexdigest()