Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit 62927f4

Browse files
authored
Create base58.py
1 parent fbeacaf commit 62927f4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

base58.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
""" base58 encoding / decoding functions """
2+
3+
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
4+
base_count = len(alphabet)
5+
6+
def encode(num):
7+
""" Returns num in a base58-encoded string """
8+
encode = ''
9+
10+
if (num < 0):
11+
return ''
12+
13+
while (num >= base_count):
14+
mod = num % base_count
15+
encode = alphabet[mod] + encode
16+
num = num // base_count
17+
18+
if (num):
19+
encode = alphabet[num] + encode
20+
21+
return encode
22+
23+
def decode(s):
24+
""" Decodes the base58-encoded string s into an integer """
25+
decoded = 0
26+
multi = 1
27+
s = s[::-1]
28+
for char in s:
29+
decoded += multi * alphabet.index(char)
30+
multi = multi * base_count
31+
32+
return decoded

0 commit comments

Comments
 (0)