/usr/lib/python3/dist-packages/bip32utils/Base58.py is in python3-bip32utils 0.0~git20170118.dd9c541-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #!/usr/bin/env python
#
# Copyright 2014 Corgan Labs
# See LICENSE.txt for distribution terms
#
from hashlib import sha256
__base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__base58_alphabet_bytes = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
__base58_radix = len(__base58_alphabet)
def __string_to_int(data):
"Convert string of bytes Python integer, MSB"
val = 0
# Python 2.x compatibility
if type(data) == str:
data = bytearray(data)
for (i, c) in enumerate(data[::-1]):
val += (256**i)*c
return val
def encode(data):
"Encode bytes into Bitcoin base58 string"
enc = ''
val = __string_to_int(data)
while val >= __base58_radix:
val, mod = divmod(val, __base58_radix)
enc = __base58_alphabet[mod] + enc
if val:
enc = __base58_alphabet[val] + enc
# Pad for leading zeroes
n = len(data)-len(data.lstrip(b'\0'))
return __base58_alphabet[0]*n + enc
def check_encode(raw):
"Encode raw bytes into Bitcoin base58 string with checksum"
chk = sha256(sha256(raw).digest()).digest()[:4]
return encode(raw+chk)
def decode(data):
"Decode Bitcoin base58 format string to bytes"
# Python 2.x compatability
if bytes != str:
data = bytes(data, 'ascii')
val = 0
for (i, c) in enumerate(data[::-1]):
val += __base58_alphabet_bytes.find(c) * (__base58_radix**i)
dec = bytearray()
while val >= 256:
val, mod = divmod(val, 256)
dec.append(mod)
if val:
dec.append(val)
return bytes(dec[::-1])
def check_decode(enc):
"Decode bytes from Bitcoin base58 string and test checksum"
dec = decode(enc)
raw, chk = dec[:-4], dec[-4:]
if chk != sha256(sha256(raw).digest()).digest()[:4]:
raise ValueError("base58 decoding checksum error")
else:
return raw
if __name__ == '__main__':
assert(__base58_radix == 58)
data = b'now is the time for all good men to come to the aid of their country'
enc = check_encode(data)
assert(check_decode(enc) == data)
|