Author

Topic: Hash to 160 (Python) (Read 229 times)

hero member
Activity: 1220
Merit: 612
OGRaccoon
July 12, 2019, 05:58:49 AM
#5
Thanks guys you solved my issue and thank you for the explanations and script.

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





legendary
Activity: 3472
Merit: 10611
July 11, 2019, 11:23:49 PM
#4
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.
HCP
legendary
Activity: 2086
Merit: 4318
July 11, 2019, 06:56:23 PM
#3
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
full member
Activity: 378
Merit: 197
July 11, 2019, 06:22:28 PM
#2
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

PS. it is made with python 2.7  (I think i had 2.7.13 when I edited that... long time ago)
hero member
Activity: 1220
Merit: 612
OGRaccoon
July 11, 2019, 03:33:14 PM
#1
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!

 Wink


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')
Jump to: