Skip to content

Instantly share code, notes, and snippets.

@circulosmeos
Last active January 6, 2025 17:36
Show Gist options
  • Save circulosmeos/ef6497fd3344c2c2508b92bb9831173f to your computer and use it in GitHub Desktop.
Save circulosmeos/ef6497fd3344c2c2508b92bb9831173f to your computer and use it in GitHub Desktop.
Easily generate the bitcoin address from the public key using Python (compatible with Python 2 and 3)
#!/usr/bin/env python
# https://en.bitcoin.it/wiki/Protocol_documentation#Addresses
import hashlib
import base58
# ECDSA bitcoin Public Key
pubkey = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6'
# See 'compressed form' at https://en.bitcoin.it/wiki/Protocol_documentation#Signatures
compress_pubkey = False
def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update( sha.digest() )
print ( "key_hash = \t" + rip.hexdigest() )
return rip.hexdigest() # .hexdigest() is hex ASCII
if (compress_pubkey):
if (ord(bytearray.fromhex(pubkey[-2:])) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pubkey[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pubkey)
# Obtain key:
key_hash = '00' + hash160(hex_str)
# Obtain signature:
sha = hashlib.sha256()
sha.update( bytearray.fromhex(key_hash) )
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]
print ( "checksum = \t" + sha.hexdigest() )
print ( "key_hash + checksum = \t" + key_hash + ' ' + checksum )
print ( "bitcoin address = \t" + (base58.b58encode( bytes(bytearray.fromhex(key_hash + checksum)) )).decode('utf-8') )
@Jr271686
Copy link

Jr271686 commented Jan 6, 2025

import os
import hashlib
import base58

Função para gerar um endereço Bitcoin a partir de uma chave privada (simplificada)

def private_key_to_btc_address(private_key_hex):
# Hash SHA-256 da chave privada
sha256_key = hashlib.sha256(bytes.fromhex(private_key_hex)).digest()

# RIPEMD-160 do hash SHA-256
ripemd160_key = hashlib.new('ripemd160', sha256_key).digest()

# Prefixo de rede Bitcoin (0x00 para Mainnet)
prefixed_key = b'\x00' + ripemd160_key

# Duplo SHA-256 para checksum
checksum = hashlib.sha256(hashlib.sha256(prefixed_key).digest()).digest()[:4]

# Endereço final
binary_address = prefixed_key + checksum
address = base58.b58encode(binary_address).decode('utf-8')

return address

Endereço-alvo para comparação

target_address = "14u4nA5sugaswb6SZgn5av2vuChdMnD9E5"

Simulação de força bruta com geração aleatória de chaves

attempts = 1000 # Número de tentativas simuladas

found = False
for i in range(attempts):
# Gerar uma chave privada aleatória de 32 bytes
private_key = os.urandom(32).hex()
generated_address = private_key_to_btc_address(private_key)

if generated_address == target_address:
    found = True
    result = (private_key, generated_address)
    break

found, result if found else "Nenhuma correspondência encontrada nas tentativas simuladas."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment