blockchain private key generator

Solutions on MaxInterview for blockchain private key generator by the best coders in the world

showing results for - "blockchain private key generator"
Serena
17 Jun 2020
1import ecdsa
2import random
3import hashlib
4
5b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
6
7def privateKeyToWif(key_hex):    
8    return base58CheckEncode(0x80, key_hex.decode('hex'))
9    
10def privateKeyToPublicKey(s):
11    sk = ecdsa.SigningKey.from_string(s.decode('hex'), curve=ecdsa.SECP256k1)
12    vk = sk.verifying_key
13    return ('\04' + sk.verifying_key.to_string()).encode('hex')
14    
15def pubKeyToAddr(s):
16    ripemd160 = hashlib.new('ripemd160')
17    ripemd160.update(hashlib.sha256(s.decode('hex')).digest())
18    return base58CheckEncode(0,ripemd160.digest())
19
20def keyToAddr(s):
21	return pubKeyToAddr(privateKeyToPublicKey(s))
22
23def base58encode(n):
24    result = ''
25    while n > 0:
26        result = b58[n%58] + result
27        n /= 58
28    return result
29
30def base58CheckEncode(version, payload):
31    s = chr(version) + payload
32    checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
33    result = s + checksum
34    leadingZeros = countLeadingChars(result, '\0')
35   
36    return '1' * leadingZeros + base58encode(base256decode(result))
37
38def base256decode(s):
39    result = 0
40    for c in s:
41        result = result * 256 + ord(c)
42    return result
43
44def countLeadingChars(s, ch):
45    count = 0
46    for c in s:
47        if c == ch:
48            count += 1
49        else:
50            break
51    return count
52
53private_key = ''.join(['%x' % random.randrange(16) for x in range(0, 64)])
54print 'Private key: ',private_key
55pubKey = privateKeyToPublicKey(private_key)
56print '\nPublic key: ',pubKey
57print '\nWif: ',privateKeyToWif(private_key)
58print '\nAddress: ',keyToAddr(private_key)
59
Chelsea
13 Jan 2017
1ty for code