added validation of input data
This commit is contained in:
parent
9003eefc79
commit
1dce8d124d
@ -11,9 +11,11 @@ from .constants import *
|
|||||||
#
|
#
|
||||||
|
|
||||||
def create_passphrase(bits=256, language='english'):
|
def create_passphrase(bits=256, language='english'):
|
||||||
entropy = os.urandom(bits // 8)
|
if bits in [128, 160, 192, 224, 256]:
|
||||||
mnemonic = create_mnemonic(entropy, language)
|
entropy = os.urandom(bits // 8)
|
||||||
return ' '.join(mnemonic[::-1])
|
mnemonic = create_mnemonic(entropy, language)
|
||||||
|
return ' '.join(mnemonic[::-1])
|
||||||
|
raise ValueError('Strength should be one of the following [128, 160, 192, 224, 256], but it is not (%d).' % bits)
|
||||||
|
|
||||||
|
|
||||||
def create_mnemonic(entropy, language='english'):
|
def create_mnemonic(entropy, language='english'):
|
||||||
@ -57,22 +59,24 @@ def add_checksum(data):
|
|||||||
|
|
||||||
def mnemonic2bytes(passphrase, language):
|
def mnemonic2bytes(passphrase, language):
|
||||||
mnemonic = passphrase.split()
|
mnemonic = passphrase.split()
|
||||||
wordlist = create_wordlist(language)
|
if len(mnemonic) in [12, 15, 18, 21, 24]:
|
||||||
codes = dict()
|
wordlist = create_wordlist(language)
|
||||||
for code, word in enumerate(wordlist):
|
codes = dict()
|
||||||
codes[word] = code
|
for code, word in enumerate(wordlist):
|
||||||
word_count = len(mnemonic)
|
codes[word] = code
|
||||||
entropy_int = None
|
word_count = len(mnemonic)
|
||||||
bit_size = word_count * 11
|
entropy_int = None
|
||||||
chk_sum_bit_len = word_count * 11 % 32
|
bit_size = word_count * 11
|
||||||
for word in mnemonic:
|
chk_sum_bit_len = word_count * 11 % 32
|
||||||
entropy_int = (entropy_int << 11) | codes[word] if entropy_int else codes[word]
|
for word in mnemonic:
|
||||||
chk_sum = entropy_int & (2 ** chk_sum_bit_len - 1)
|
entropy_int = (entropy_int << 11) | codes[word] if entropy_int else codes[word]
|
||||||
entropy_int = entropy_int >> chk_sum_bit_len
|
chk_sum = entropy_int & (2 ** chk_sum_bit_len - 1)
|
||||||
entropy = entropy_int.to_bytes((bit_size - chk_sum_bit_len) // 8, byteorder="big")
|
entropy_int = entropy_int >> chk_sum_bit_len
|
||||||
ent_hash = hashlib.sha256(entropy).hexdigest()
|
entropy = entropy_int.to_bytes((bit_size - chk_sum_bit_len) // 8, byteorder="big")
|
||||||
fb = unhexlify(ent_hash)[0]
|
ent_hash = hashlib.sha256(entropy).hexdigest()
|
||||||
assert (fb >> (8 - chk_sum_bit_len)) & chk_sum
|
fb = unhexlify(ent_hash)[0]
|
||||||
return entropy
|
assert (fb >> (8 - chk_sum_bit_len)) & chk_sum
|
||||||
|
return entropy
|
||||||
|
raise ValueError('Number of words must be one of the following: [12, 15, 18, 21, 24], but it is not (%d).' % len(mnemonic))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user