Bitcoin Forum
June 19, 2024, 08:11:45 AM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Hash to 160 (Python)  (Read 229 times)
DaCryptoRaccoon (OP)
Hero Member
*****
Offline Offline

Activity: 1219
Merit: 612


OGRaccoon


View Profile
July 11, 2019, 07:33:14 PM
Last edit: July 12, 2019, 10:00:19 AM by MagicByt3
 #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')

┏━━━━━━━━━━━━━━━━━┓
┃   💎 Mine Solo with CKPool 💎    ┃
┃    ➤ Hit Blocks on Your Own!      ┃
┃ ███▓▓  ███▓▓  ███▓▓  ███▓▓┃
aplistir
Full Member
***
Offline Offline

Activity: 378
Merit: 197



View Profile
July 11, 2019, 10:22:28 PM
Merited by DaCryptoRaccoon (1)
 #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)

My Address: 121f7zb2U4g9iM4MiJTDhEzqeZGHzq5wLh
HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
July 11, 2019, 10:56:23 PM
Merited by DaCryptoRaccoon (1)
 #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

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
pooya87
Legendary
*
Offline Offline

Activity: 3486
Merit: 10665



View Profile
July 12, 2019, 03:23:49 AM
Merited by ABCbits (1)
 #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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
DaCryptoRaccoon (OP)
Hero Member
*****
Offline Offline

Activity: 1219
Merit: 612


OGRaccoon


View Profile
July 12, 2019, 09: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






┏━━━━━━━━━━━━━━━━━┓
┃   💎 Mine Solo with CKPool 💎    ┃
┃    ➤ Hit Blocks on Your Own!      ┃
┃ ███▓▓  ███▓▓  ███▓▓  ███▓▓┃
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!