Skip to content

Instantly share code, notes, and snippets.

@retrage
Last active August 29, 2015 14:27
Show Gist options
  • Save retrage/1ba9cb702cb512b9ba1e to your computer and use it in GitHub Desktop.
Save retrage/1ba9cb702cb512b9ba1e to your computer and use it in GitHub Desktop.
Base58 encoder/decoder
#!/usr/bin/python
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
base = len(alphabet)
def encode_base58(input_bin):
if input_bin == 0x00:
return alphabet[0]
res = ""
while input_bin > 0:
remain = input_bin % base
input_bin = input_bin / base
res = alphabet[remain] + res
return res
def decode_base58(input_str):
res = 0x00
num = len(input_str)
multi = 1
for c in reversed(input_str):
res = res + multi * alphabet.index(c)
multi = multi * base
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment