def get_base58_shares(hex_str, m, n): """ Convert the hex string to a base58 string and split it into "n" shares. """ base58_str = change_charset(hex_str, string.hexdigits[0:16], base58_chars) shares = BitcoinToB58SecretSharer.split_secret(base58_str, m, n) return shares
def pk_to_address(pk, version_byte=0, num_leading_zeros=1): # 0 for main net step_1 = pk pk = unhexlify(step_1) step_2 = sha256(pk) # hexdigest to get hex step_3 = hashlib.new('ripemd160', step_2.digest()) step_4 = '00' + step_3.hexdigest() # if main net, num_leading_zeros step_5 = sha256(unhexlify(step_4)) step_6 = sha256(step_5.digest()) step_7 = checksum = step_6.hexdigest()[:8] # first 4 bytes step_8 = step_4 + checksum b58_s = change_charset(step_8, HEX_KEYSPACE, B58_KEYSPACE) # pybitcoin lib return B58_KEYSPACE[0] * num_leading_zeros + b58_s
def b58check_encode(bin_s, version_byte=0): """ Takes in a binary string and converts it to a base 58 check string. """ # append the version byte to the beginning bin_s = chr(int(version_byte)) + bin_s # calculate the number of leading zeros num_leading_zeros = len(re.match(r"^\x00*", bin_s).group(0)) # add in the checksum add the end bin_s = bin_s + bin_checksum(bin_s) # convert from b2 to b16 hex_s = hexlify(bin_s) # convert from b16 to b58 b58_s = change_charset(hex_s, HEX_KEYSPACE, B58_KEYSPACE) return B58_KEYSPACE[0] * num_leading_zeros + b58_s
def b58check_encode(bin_s, version_byte=0): """ Takes in a binary string and converts it to a base 58 check string. """ # append the version byte to the beginning bin_s = chr(int(version_byte)) + bin_s # calculate the number of leading zeros num_leading_zeros = len(re.match(r'^\x00*', bin_s).group(0)) # add in the checksum add the end bin_s = bin_s + bin_checksum(bin_s) # convert from b2 to b16 hex_s = hexlify(bin_s) # convert from b16 to b58 b58_s = change_charset(hex_s, HEX_KEYSPACE, B58_KEYSPACE) return B58_KEYSPACE[0] * num_leading_zeros + b58_s
def b58check_unpack(b58_s): """ Takes in a base 58 check string and returns: the version byte, the original encoded binary string, and the checksum. """ num_leading_zeros = len(re.match(r'^1*', b58_s).group(0)) # convert from b58 to b16 hex_s = change_charset(b58_s, B58_KEYSPACE, HEX_KEYSPACE) # if an odd number of hex characters are present, add a zero to the front if len(hex_s) % 2 == 1: hex_s = "0" + hex_s # convert from b16 to b2 bin_s = binascii.unhexlify(hex_s) # add in the leading zeros bin_s = '\x00' * num_leading_zeros + bin_s # make sure the newly calculated checksum equals the embedded checksum newly_calculated_checksum = bin_checksum(bin_s[:-4]) embedded_checksum = bin_s[-4:] assert(newly_calculated_checksum == embedded_checksum) # return values version_byte = bin_s[:1] encoded_value = bin_s[1:-4] checksum = bin_s[-4:] return version_byte, encoded_value, checksum
def b58check_unpack(b58_s): """ Takes in a base 58 check string and returns: the version byte, the original encoded binary string, and the checksum. """ num_leading_zeros = len(re.match(r'^1*', b58_s).group(0)) # convert from b58 to b16 hex_s = change_charset(b58_s, B58_KEYSPACE, HEX_KEYSPACE) # if an odd number of hex characters are present, add a zero to the front if len(hex_s) % 2 == 1: hex_s = "0" + hex_s # convert from b16 to b2 bin_s = unhexlify(hex_s) # add in the leading zeros bin_s = '\x00' * num_leading_zeros + bin_s # make sure the newly calculated checksum equals the embedded checksum newly_calculated_checksum = bin_checksum(bin_s[:-4]) embedded_checksum = bin_s[-4:] if not (newly_calculated_checksum == embedded_checksum): raise ValueError('b58check value has an invalid checksum') # return values version_byte = bin_s[:1] encoded_value = bin_s[1:-4] checksum = bin_s[-4:] return version_byte, encoded_value, checksum
# Atividade - 12 # Leet é uma forma de se escrever o alfabeto latino usando outros # símbolos em lugar das letras, como números por exemplo. A própria # palavra leet admite muitas variações, como l33t ou 1337. O uso do # leet reflete uma subcultura relacionada ao mundo dos jogos de # computador e internet, sendo muito usada para confundir os # iniciantes e afrmar-se como parte de um grupo. Pesquise sobre as # principais formas de traduzir as letras. Depois, faça um programa # que peça uma texto e transforme-o para a grafa leet speak. # Desafo: não use loops nem desvios condicionais. from utilitybelt import change_charset texto = input("Digite um texto: ") alfabeto = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " chaves = "4bcd3ƒgh1?k£mnopqr57µvwxyz4BCD3FGHI?KLMNOP0R57UVWXYZ_" formatada = change_charset(texto, alfabeto, chaves) print(formatada) ##Gaspar
# 12 Leet é uma forma de se escrever o alfabeto latino usando outros # símbolos em lugar das letras, como números por exemplo. A própria # palavra leet admite muitas variações, como l33t ou 1337. O uso do # leet reflete uma subcultura relacionada ao mundo dos jogos de # computador e internet, sendo muito usada para confundir os # iniciantes e afrmar-se como parte de um grupo. Pesquise sobre as # principais formas de traduzir as letras. Depois, faça um programa # que peça uma texto e transforme-o para a grafa leet speak. # Desafo: não use loops nem desvios condicionais. from utilitybelt import change_charset print("Informe uma frase") frase = input() origspace = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " keyspace = "4bcd3fgh1?k£mnopqr57uvwxyz4BCD3FGHI?KLMNOPQR57UVWXYZ_" trad = change_charset(frase, origspace, keyspace) print(trad)
def combine_shares(shamir_shares, encoding=Encoding.BIP39, language="english"): """ Combine the shamir39 shares to get BIP39 mnemonics @param shamir_shares @return recovered_key: secret key recovered by combining the shamir shares """ hex_shamir_shares = list() num_required_shares = -1 share_index = 1 if encoding == Encoding.BIP39: for share in shamir_shares: words = share.split(" ") # Check version if words[0] != VERSION: abort(400, {'message': "Incompatible version!"}) # Extract params from prefix m_bin_str = "" index_bin_str = "" param_end_index = 1 for word in words[1:]: word_index = WORDLIST[language].index(word) if word_index < 0: error_message = "Invalid word found in the mnemonics: " + word abort(400, {'message': error_message}) word_bin = "{0:b}".format(word_index) word_bin = word_bin.rjust(11, '0') end_of_param = word_bin[0] m_bin_str = m_bin_str + word_bin[1:6] index_bin_str = index_bin_str + word_bin[6:11] param_end_index += 1 if end_of_param == "0": break # Get m and index m = int(m_bin_str, 2) index = hex(int(index_bin_str, 2)).split("x")[1] # Set the required number of shares if num_required_shares == -1: num_required_shares = m # Check if the required number of shares is met if len(shamir_shares) < num_required_shares: abort(400, {'message': "Insufficient shares provided!"}) # Check consistency of m param in all shares - if m != num_required_shares: abort(400, {'message': 'Inconsistent M parameter in the shares!'}) bin_share = mnemonics_to_bin(words[param_end_index:], language) hex_share = hex(int(bin_share, 2)) hex_share = hex_share.split("x")[1] hex_share = str(index) + "-" + hex_share hex_shamir_shares.append(hex_share) share_index += 1 recovered_key_hex = SecretSharer.recover_secret(hex_shamir_shares) else: recovered_key_base58 = BitcoinToB58SecretSharer.recover_secret( shamir_shares) recovered_key_hex = change_charset(recovered_key_base58, base58_chars, string.hexdigits[0:16]) recovered_key_bin = bin(int(recovered_key_hex, 16)).split("b")[1] recovered_key = bin_to_mnemonics(recovered_key_bin, language) recovered_key = " ".join(recovered_key) return recovered_key