Last active
November 8, 2017 14:52
-
-
Save zimolzak/945af9423e58ed37deeae37f0f76c593 to your computer and use it in GitHub Desktop.
Random valid-hash Bitcoin address, highly UNlikely to be spendable (have valid keys, or private key known by someone)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
from hashlib import sha256 | |
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' | |
# No big I, big O, digit 0, or little l. 26 * 2 + 10 - 4 = 58. | |
base_count = len(digits58) | |
def encode_base58(bitcoin_bytes): | |
num = int.from_bytes(bitcoin_bytes, 'big') | |
encoding = '' | |
if (num < 0): | |
return '' | |
while (num >= base_count): | |
remainder = num % base_count | |
encoding = digits58[remainder] + encoding | |
num = num // base_count | |
if (num): | |
encoding = digits58[num] + encoding | |
return encoding | |
def rand_address(): | |
randints = [0] | |
num_bytes = 20 | |
for i in range(num_bytes): | |
randints.append(random.randint(0,255)) | |
randbytes = bytes(randints) | |
hash_full = sha256(sha256(randbytes).digest()).digest() | |
hash_4 = hash_full[:4] | |
bytes_with_hash = randbytes + hash_4 | |
e = encode_base58(bytes_with_hash) | |
return '1' + e | |
#### Functions decode_base58 and check_bc adapted from: | |
#### https://rosettacode.org/wiki/Bitcoin/address_validation#Python | |
def decode_base58(bc, length): | |
n = 0 | |
for char in bc: | |
n = n * base_count + digits58.index(char) | |
return n.to_bytes(length, 'big') | |
def check_bc(bc): | |
bcbytes = decode_base58(bc, 25) | |
return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4] | |
######## | |
for ad, expected in [('1AGNa15ZQXAZUgFiqJ3i7Z2DPU2J6hW62i', False), | |
("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j", True), | |
("123456789ABCDEFGHJKLMNPQRSTUVWXYZa", False), | |
("1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a", False)]: | |
assert check_bc(ad) == expected | |
fake_addr = rand_address() | |
print("Addr: ", fake_addr) | |
print("Valid:", check_bc(fake_addr)) | |
assert check_bc(fake_addr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment