-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext2bip.py
More file actions
executable file
·46 lines (37 loc) · 1.28 KB
/
text2bip.py
File metadata and controls
executable file
·46 lines (37 loc) · 1.28 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
#!/usr/local/bin/python3
import hashlib, sys
def sha256(data):
if type(data) == type(""):
data = data.encode("utf-8")
return hashlib.sha256(data).digest()
def getwordlist():
return open("english.txt","r").read().split("\n")
def getbinarystring(digest):
bits = ""
for byte in digest:
bits += bin(byte)[2:].rjust(8,"0")
return bits
def getmnemonicphrase(text):
# sha256 of the text
digest = sha256(text)
# calculate the hash bits
hashbits = getbinarystring(sha256(digest))[:8]
# string of bits from the digest
binarystring = getbinarystring(digest) + hashbits
# wordlist of mnemonics
wordlist = getwordlist()
# bits spllited in groups of 11 bits
entropygroups = [binarystring[i:i+11] for i in range(0,len(binarystring),11)]
# convert these groups of bits into integers that will be used as indexes
mnemonicindexes = map(lambda x : int(x,2),entropygroups)
# map mnemonics
mnemonics = list(map(lambda x : wordlist[x],mnemonicindexes))
return " ".join(mnemonics)
def main():
text = input("Please, enter your secret text below:\n")
result = getmnemonicphrase(text)
print("Your mnemonic phrase is:")
print(result)
if __name__ == "__main__":
# execute only if run as a script
main()