-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbase58.py
More file actions
37 lines (27 loc) · 781 Bytes
/
Copy pathbase58.py
File metadata and controls
37 lines (27 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import hashlib
b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
def base58_encode(n):
result = ''
while n > 0:
result = b58[n % 58] + result
n /= 58
return result
def count_leading_chars(s, ch):
count = 0
for c in s:
if c == ch:
count += 1
else:
break
return count
def base256_decode(s):
result = 0
for c in s:
result = result * 256 + ord(c)
return result
def base58_check_encode(version, payload):
s = chr(version) + payload
checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
result = s + checksum
leading_zeros = count_leading_chars(result, '\0')
return '1' * leading_zeros + base58_encode(base256_decode(result))