Changeset View
Changeset View
Standalone View
Standalone View
contrib/testgen/base58.py
# Copyright (c) 2012-2016 The Bitcoin Core developers | # Copyright (c) 2012-2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
''' | ''' | ||||
Bitcoin base58 encoding and decoding. | Bitcoin base58 encoding and decoding. | ||||
Based on https://bitcointalk.org/index.php?topic=1026.0 (public domain) | Based on https://bitcointalk.org/index.php?topic=1026.0 (public domain) | ||||
''' | ''' | ||||
import hashlib | import hashlib | ||||
Show All 18 Lines | |||||
b58chars = __b58chars | b58chars = __b58chars | ||||
def b58encode(v): | def b58encode(v): | ||||
""" encode v, which is a string of bytes, to base58. | """ encode v, which is a string of bytes, to base58. | ||||
""" | """ | ||||
long_value = 0 | long_value = 0 | ||||
for (i, c) in enumerate(v[::-1]): | for (i, c) in enumerate(v[::-1]): | ||||
long_value += (256**i) * ord(c) | if isinstance(c, str): | ||||
c = ord(c) | |||||
long_value += (256**i) * c | |||||
result = '' | result = '' | ||||
while long_value >= __b58base: | while long_value >= __b58base: | ||||
div, mod = divmod(long_value, __b58base) | div, mod = divmod(long_value, __b58base) | ||||
result = __b58chars[mod] + result | result = __b58chars[mod] + result | ||||
long_value = div | long_value = div | ||||
result = __b58chars[long_value] + result | result = __b58chars[long_value] + result | ||||
# Bitcoin does a little leading-zero-compression: | # Bitcoin does a little leading-zero-compression: | ||||
# leading 0-bytes in the input become leading-1s | # leading 0-bytes in the input become leading-1s | ||||
nPad = 0 | nPad = 0 | ||||
for c in v: | for c in v: | ||||
if c == '\0': | if c == 0: | ||||
nPad += 1 | nPad += 1 | ||||
else: | else: | ||||
break | break | ||||
return (__b58chars[0] * nPad) + result | return (__b58chars[0] * nPad) + result | ||||
def b58decode(v, length=None): | def b58decode(v, length=None): | ||||
""" decode v into a string of len bytes | """ decode v into a string of len bytes | ||||
""" | """ | ||||
long_value = 0 | long_value = 0 | ||||
for (i, c) in enumerate(v[::-1]): | for i, c in enumerate(v[::-1]): | ||||
long_value += __b58chars.find(c) * (__b58base**i) | pos = __b58chars.find(c) | ||||
assert pos != -1 | |||||
long_value += pos * (__b58base**i) | |||||
result = bytes() | result = bytes() | ||||
while long_value >= 256: | while long_value >= 256: | ||||
div, mod = divmod(long_value, 256) | div, mod = divmod(long_value, 256) | ||||
result = chr(mod) + result | result = chr(mod) + result | ||||
long_value = div | long_value = div | ||||
result = chr(long_value) + result | result = chr(long_value) + result | ||||
nPad = 0 | nPad = 0 | ||||
for c in v: | for c in v: | ||||
if c == __b58chars[0]: | if c == __b58chars[0]: | ||||
nPad += 1 | nPad += 1 | ||||
else: | continue | ||||
break | break | ||||
result = chr(0) * nPad + result | result = bytes(nPad) + result | ||||
if length is not None and len(result) != length: | if length is not None and len(result) != length: | ||||
return None | return None | ||||
return result | return result | ||||
def checksum(v): | def checksum(v): | ||||
"""Return 32-bit checksum based on SHA256""" | """Return 32-bit checksum based on SHA256""" | ||||
Show All 36 Lines |