forked from nlitsme/python-bcutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base58.py
62 lines (49 loc) · 1.15 KB
/
base58.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import print_function, division
from binascii import a2b_hex, b2a_hex
"""
By Willem Hengeveld <itsme@xs4all.nl>
base58 encode and decode
"""
charset= "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def encode(data):
def nbytes(x):
return (x.bit_length()+7)//8
res= ""
if type(data)==bytes:
bytelen= len(data)
if bytelen:
data= int(b2a_hex(data), 16)
else:
data= 0
nrzeros= bytelen - nbytes(data)
else:
data= int(data)
bytelen= nbytes(data)
nrzeros= 0
while data:
res += charset[data%58]
data //= 58
res += charset[0] * nrzeros
return res[::-1]
def decode(enc):
num= 0
nrzeros= 0
for c in enc:
if c==charset[0]:
nrzeros += 1
else:
break
for c in enc:
digit= charset.find(c)
if digit<0:
break
num *= 58
num += digit
if num:
hexstr= "%x" % num
if len(hexstr)%2:
hexstr= "0" + hexstr
else:
hexstr= ""
hexstr= "00" * nrzeros + hexstr
return a2b_hex(hexstr)