<s> import sys , os <EOL> import binascii <EOL> import hashlib <EOL> if sys . version_info . major == <NUM_LIT:3> : <EOL> string_types = ( str ) <EOL> string_or_bytes_types = ( str , bytes ) <EOL> int_types = ( int , float ) <EOL> code_strings = { <EOL> <NUM_LIT:2> : '<STR_LIT>' , <EOL> <NUM_LIT:10> : '<STR_LIT>' , <EOL> <NUM_LIT:16> : '<STR_LIT>' , <EOL> <NUM_LIT:32> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' . join ( [ chr ( x ) for x in range ( <NUM_LIT> ) ] ) <EOL> } <EOL> def bin_dbl_sha256 ( s ) : <EOL> bytes_to_hash = from_string_to_bytes ( s ) <EOL> return hashlib . sha256 ( hashlib . sha256 ( bytes_to_hash ) . digest ( ) ) . digest ( ) <EOL> def lpad ( msg , symbol , length ) : <EOL> if len ( msg ) >= length : <EOL> return msg <EOL> return symbol * ( length - len ( msg ) ) + msg <EOL> def get_code_string ( base ) : <EOL> if base in code_strings : <EOL> return code_strings [ base ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def changebase ( string , frm , to , minlen = <NUM_LIT:0> ) : <EOL> if frm == to : <EOL> return lpad ( string , get_code_string ( frm ) [ <NUM_LIT:0> ] , minlen ) <EOL> return encode ( decode ( string , frm ) , to , minlen ) <EOL> def bin_to_b58check ( inp , magicbyte = <NUM_LIT:0> ) : <EOL> inp_fmtd = from_int_to_byte ( int ( magicbyte ) ) + inp <EOL> leadingzbytes = <NUM_LIT:0> <EOL> for x in inp_fmtd : <EOL> if x != <NUM_LIT:0> : <EOL> break <EOL> leadingzbytes += <NUM_LIT:1> <EOL> checksum = bin_dbl_sha256 ( inp_fmtd ) [ : <NUM_LIT:4> ] <EOL> return '<STR_LIT:1>' * leadingzbytes + changebase ( inp_fmtd + checksum , <NUM_LIT> , <NUM_LIT> ) <EOL> def bytes_to_hex_string ( b ) : <EOL> if isinstance ( b , str ) : <EOL> return b <EOL> return '<STR_LIT>' . join ( '<STR_LIT>' . format ( y ) for y in b ) <EOL> def safe_from_hex ( s ) : <EOL> return bytes . fromhex ( s ) <EOL> def from_int_representation_to_bytes ( a ) : <EOL> return bytes ( str ( a ) , '<STR_LIT:utf-8>' ) <EOL> def from_int_to_byte ( a ) : <EOL> return bytes ( [ a ] ) <EOL> def from_byte_to_int ( a ) : <EOL> return a <EOL> def from_string_to_bytes ( a ) : <EOL> return a if isinstance ( a , bytes ) else bytes ( a , '<STR_LIT:utf-8>' ) <EOL> def safe_hexlify ( a ) : <EOL> return str ( binascii . hexlify ( a ) , '<STR_LIT:utf-8>' ) <EOL> def encode ( val , base , minlen = <NUM_LIT:0> ) : <EOL> base , minlen = int ( base ) , int ( minlen ) <EOL> code_string = get_code_string ( base ) <EOL> result_bytes = bytes ( ) <EOL> while val > <NUM_LIT:0> : <EOL> curcode = code_string [ val % base ] <EOL> result_bytes = bytes ( [ ord ( curcode ) ] ) + result_bytes <EOL> val //= base <EOL> pad_size = minlen - len ( result_bytes ) <EOL> padding_element = b'<STR_LIT:\x00>' if base == <NUM_LIT> else b'<STR_LIT:1>' if base == <NUM_LIT> else b'<STR_LIT:0>' <EOL> if ( pad_size > <NUM_LIT:0> ) : <EOL> result_bytes = padding_element * pad_size + result_bytes <EOL> result_string = '<STR_LIT>' . join ( [ chr ( y ) for y in result_bytes ] ) <EOL> result = result_bytes if base == <NUM_LIT> else result_string <EOL> return result <EOL> def decode ( string , base ) : <EOL> if base == <NUM_LIT> and isinstance ( string , str ) : <EOL> string = bytes ( bytearray . fromhex ( string ) ) <EOL> base = int ( base ) <EOL> code_string = get_code_string ( base ) <EOL> result = <NUM_LIT:0> <EOL> if base == <NUM_LIT> : <EOL> def extract ( d , cs ) : <EOL> return d <EOL> else : <EOL> def extract ( d , cs ) : <EOL> return cs . find ( d if isinstance ( d , str ) else chr ( d ) ) <EOL> if base == <NUM_LIT:16> : <EOL> string = string . lower ( ) <EOL> while len ( string ) > <NUM_LIT:0> : <EOL> result *= base <EOL> result += extract ( string [ <NUM_LIT:0> ] , code_string ) <EOL> string = string [ <NUM_LIT:1> : ] <EOL> return result <EOL> def random_string ( x ) : <EOL> return str ( os . urandom ( x ) ) </s>
