Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: DaCryptoRaccoon on July 11, 2019, 07:33:14 PM



Title: Hash to 160 (Python)
Post by: DaCryptoRaccoon on July 11, 2019, 07:33:14 PM
Trying to convert pub key to corresponding hash 160.

input.txt would have the public addresses inside
output would be the hash160's but the issue is the hash 160's that come back do not match on the explorer.

Is there a step that is missing from the conversion?  I read that I need possibly base58 check

Thanks in advance!

 ;)


Code:
import hashlib,binascii

# Set Output file
with open('OUT.txt', 'w') as result:

# Set input addresses
    for word in open('input.txt').read().split():

     # Check encoding and set Algorithm choice SHA1, SHA224, SHA256, SHA384, SHA512, RIPEMD160, MD5,
        h = hashlib.new('ripemd160', word.encode('utf-8')).digest()
        result.write(binascii.hexlify(h) + '\n')


Title: Re: Hash to 160 (Python)
Post by: aplistir on July 11, 2019, 10:22:28 PM
Here is a link to a python code that creates bitcoin addresses

It was an old code that I found from somewhere in the internet couple of years ago and then edited it so that it now supports the compressed address format. I think I also changed the RNG to a better one.
But you can see how bitcoins base58Check encoding, and hashing can be implemented quite easily in python.

https://pastebin.com/6yw2BtDM (https://pastebin.com/6yw2BtDM)

PS. it is made with python 2.7  (I think i had 2.7.13 when I edited that... long time ago)


Title: Re: Hash to 160 (Python)
Post by: HCP on July 11, 2019, 10:56:23 PM
Trying to convert pub key to corresponding hash 160.
Sounds good...


Quote
input.txt would have the public addresses inside
Errr.... addresses or pubkeys? You cannot go from an address -> pubkey. As the address is derived by hashing the pubkey, which is a one-way function. So you can only go pubkey -> address.


Also, in your code you are only using the SHA256 algorithm... There is no RIPEMD-160, so even if it is pubkeys that you have in the input file, you still wouldn't see the matching RIPEMD160.

Have a look at this: http://gobittest.appspot.com/Address

It shows step by step how to get from private key -> pubkey -> address and what hashes are done and when.

To see the Hash160:
Privkey -> Pubkey -> SHA-256 -> RIPEMD-160


Title: Re: Hash to 160 (Python)
Post by: pooya87 on July 12, 2019, 03:23:49 AM
Quote
h = hashlib.new('sha256', word.encode('utf-8')).digest()

usually when you write down a public key it is in hexadecimal format not UTF8 (unless you have explicitly converted them to UTF8!), so you have to convert the string from hex to bytes (or binary?) then pass it to your hashlib to perform SHA256 on it.
then your "h" is the 32 byte SHA256 digest (hash result) you then want to perform RIPEMD160 hash function on this result as HCP said to get the "HASH160" that you are looking for.


Title: Re: Hash to 160 (Python)
Post by: DaCryptoRaccoon on July 12, 2019, 09:58:49 AM
Thanks guys you solved my issue and thank you for the explanations and script.

@pooya87  I ran out of merits so IOU 1+ merit :)