forked from keepkey/python-keepkey
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnemonic_check.py
More file actions
Latest commit
executable file
·76 lines (57 loc) · 2.27 KB
/
Copy pathmnemonic_check.py
File metadata and controls
executable file
·76 lines (57 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
from __future__ importprint_function
importbinascii
importhashlib
importmnemonic
__doc__='''
Use this script to cross-check that KeepKey generated valid
mnemonic sentence for given internal (KeepKey-generated)
and external (computer-generated) entropy.
Keep in mind that you're entering secret information to this script.
Leaking of these information may lead to stealing your bitcoins
from your wallet! We strongly recommend to run this script only on
highly secured computer (ideally live linux distribution
without an internet connection).
'''
# Python2 vs Python3
try:
input=raw_input
exceptNameError:
pass
defgenerate_entropy(strength, internal_entropy, external_entropy):
'''
strength - length of produced seed. One of 128, 192, 256
random - binary stream of random data from external HRNG
'''
ifstrengthnotin (128, 192, 256):
raiseException("Invalid strength")
ifnotinternal_entropy:
raiseException("Internal entropy is not provided")
iflen(internal_entropy) <32:
raiseException("Internal entropy too short")
ifnotexternal_entropy:
raiseException("External entropy is not provided")
iflen(external_entropy) <32:
raiseException("External entropy too short")
entropy=hashlib.sha256(internal_entropy+external_entropy).digest()
entropy_stripped=entropy[:strength//8]
iflen(entropy_stripped) *8!=strength:
raiseException("Entropy length mismatch")
returnentropy_stripped
defmain():
print(__doc__)
comp=binascii.unhexlify(input("Please enter computer-generated entropy (in hex): ").strip())
trzr=binascii.unhexlify(input("Please enter KeepKey-generated entropy (in hex): ").strip())
word_count=int(input("How many words your mnemonic has? "))
strength=word_count*32//3
entropy=generate_entropy(strength, trzr, comp)
words=mnemonic.Mnemonic('english').to_mnemonic(entropy)
ifnotmnemonic.Mnemonic('english').check(words):
print("Mnemonic is invalid")
return
iflen(words.split(' ')) !=word_count:
print("Mnemonic length mismatch!")
return
print("Generated mnemonic is:", words)
if__name__=='__main__':
main()