Cryptography BIP39校验和有时失败
我有这段代码,它将用于根据实现助记符短语生成器。 问题是,大多数情况下校验和是不正确的,但在某些情况下它是有效的,这取决于给定的熵。(我用它来测试我的校验和) 观察到以下情况: 使用了128位的熵Cryptography BIP39校验和有时失败,cryptography,python-3.7,bitcoin,checksum,cryptocurrency,Cryptography,Python 3.7,Bitcoin,Checksum,Cryptocurrency,我有这段代码,它将用于根据实现助记符短语生成器。 问题是,大多数情况下校验和是不正确的,但在某些情况下它是有效的,这取决于给定的熵。(我用它来测试我的校验和) 观察到以下情况: 使用了128位的熵 Correct Entropy: 10111101100010110111100011101111111110100010000101111110100101100000001100111111001100010010010011110110011010010010001011011000 Chec
Correct
Entropy: 10111101100010110111100011101111111110100010000101111110100101100000001100111111001100010010010011110110011010010010001011011000
Checksum: 1110
Incorrect
Entropy: 01011010000000110011001001001001001110100011100101010001001100111001111111000110000000011011110111011000011001010111001101111100
My checksum: 1010
Iancoleman checksum:1110
第一个是成功的案例,但第二个失败了。下面你可以找到我的函数
我错过了什么
def fill_bits(binary, bits):
if len(binary) < bits:
return "0" * (bits - len(binary)) + binary
return binary
# generate a given number of entropy bits
def generate_entropy(bits=256):
if bits < 128 or bits > 256:
raise EntropyRangeExceeded
entropybits = bin(int.from_bytes(os.urandom(bits // 8), byteorder=sys.byteorder))[2:]
return fill_bits(entropybits, bits)
# returns the sha256 hash of the given input
def sha256(_input):
return hashlib.sha256(_input.encode("utf-8")).hexdigest()
# returns the checksum of the input hash
# checksum is given by the first (entropy length / 32)
# bits of the sha256 hash applied on entropy bits
def get_checksum(_entropy):
entropy_length = len(_entropy) // 32
return bin(int(sha256(_entropy), 16))[2:][:entropy_length]
def fill_位(二进制,位):
如果len(二进制)<位:
返回“0”*(位-len(二进制))+二进制
返回二进制
#生成给定数量的熵位
def生成_熵(位=256):
如果位<128或位>256:
超出上升入口范围
entropybits=bin(int.from_字节(os.uradom(bits//8),byteorder=sys.byteorder))[2:]
返回填充位(entropybits,bits)
#返回给定输入的sha256哈希值
def sha256(_输入):
返回hashlib.sha256(_input.encode(“utf-8”)).hexdigest()
#返回输入哈希的校验和
#校验和由第一个(熵长度/32)给出
#应用于熵位的sha256哈希的位
def get_校验和(_熵):
熵长度=len(\u熵)//32
返回bin(int(sha256(_熵),16))[2:[:熵长度]
在sha256
中,哈希计算错误。不能执行Utf8编码。相反,熵必须表示为字节数组(请参见to_bytes
),哈希必须由此生成:
import hashlib
def sha256(_entropy):
entBytes = int(_entropy, 2).to_bytes(len(_entropy) // 8, byteorder='big')
return hashlib.sha256(entBytes).hexdigest()
此外,哈希必须用前导0值填充到256位的长度(请参见zfill
),以便在校验和中也考虑前导0值:
def get_checksum(_entropy):
entropy_length = len(_entropy) // 32
return bin(int(sha256(_entropy), 16))[2:].zfill(256)[:entropy_length];
示例1,来自,步骤4:
示例2,您的第二个示例:
_entropy = '01011010000000110011001001001001001110100011100101010001001100111001111111000110000000011011110111011000011001010111001101111100'
print(get_checksum(_entropy)) # 1110
示例3,前导0值,与以下结果进行比较:
似乎我误解了如何正确地散列熵以获得校验和。谢谢你的帮助。
_entropy = '01011010000000110011001001001001001110100011100101010001001100111001111111000110000000011011110111011000011001010111001101111100'
print(get_checksum(_entropy)) # 1110
_entropy = '10111101100011110111100011101111111110100010000101111110100101100000001100111111001100010010010011110110011011010010001011011000'
print(get_checksum(_entropy)) # 0010