尝试生成随机短语,但不断出现错误:溢出错误:int太大,无法转换
尝试了numpy数组,但它没有解决这个问题,尝试了int32和64,但仍然得到同样的错误,代码与预设的单词数组一起工作,但当它们是随机生成的时,它不起作用,有什么想法可以增加int的大小吗?
谢谢
import itertools, hashlib
my_list = ['expect', 'push', 'live', 'math', 'truth', 'fashion', 'accident', 'uniform', 'midnight', 'provide', 'distance', 'lion', 'during', 'destroy', 'tennis', 'adult', 'deal', 'refuse', 'phone', 'latin', 'minute', 'mosquito', 'match', 'toilet', 'sell', 'wrap', 'tell', 'game', 'pistol', 'choice','fog', 'stairs', 'wing', 'theme', 'axis', 'turn', 'proud', 'face', 'awake', 'convince']
combs = itertools.permutations(my_list, 11)
counter = 0
file_counter = 1
entropy_possible = []
for item in combs:
counter += 1
line = ' '.join(item) + ' ?' + '\n'
seed_phrase = line.split(" ")
english = open("english.txt")
word_list = english.read().split("\n")
english.close()
seed_phrase_index = [word_list.index(word) if word != "?" else word for word in item]
seed_phrase_binary = [format(number, "011b") if number != "?" else number for number in seed_phrase_index]
num_missing_bits = int(11-(1/3)*(len(item)))
missing_bits_possible = [bin(x)[2:].rjust(num_missing_bits, "0") for x in range(2**num_missing_bits)]
entropy_possible = ["".join(seed_phrase_binary[:-1])+bits for bits in missing_bits_possible]
checksum = [format(hashlib.sha256(int(entropy, 2).to_bytes(len(entropy) // 8, byteorder="big")).digest()[0],"08b")[:11-num_missing_bits] for entropy in entropy_possible]
last_word_bits = [i + j for i, j in zip(missing_bits_possible, checksum)]
last_word = [word_list[int(bits, 2)] for bits in last_word_bits]
print(last_word)
1条答案
按热度按时间67up9zun1#
int()
函数将二进制字符串转换为整数,该函数导致error
。random.sample()
。random.choices()
函数从列表中随机选择单词并直接生成短语,它避免了排列和二进制转换的需要。