-
Notifications
You must be signed in to change notification settings - Fork 0
/
diceware.py
83 lines (67 loc) · 2 KB
/
diceware.py
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
77
78
79
80
import argparse
import secrets
import sys
from time import sleep
import pyperclip as pyperclip
d = {}
def loadFile(file="default.txt"):
global d
with open(file, 'r') as f:
for line in f:
line = line.split(" ")
key = str(line[0])
if key.isdigit():
val = str(line[-1]).replace("\n", "")
d[key] = val
if not d:
raise RuntimeError("[-] Dict not populated!")
def generateRandomNumberInString():
generated = 0
while generated not in range(1, 7):
generated = secrets.randbelow(7)
return str(generated)
def generateWord(dictKeyLen=5):
global d
key_gen = ""
for i in range(dictKeyLen):
key_gen += generateRandomNumberInString()
return d[key_gen]
def generatePassphrase(file="default.txt", separator=" ", passPhraseWords = 5):
passphrase = ""
loadFile(file)
print("[+] Generating passphrase...")
for i in range(passPhraseWords):
if passphrase != "":
passphrase += separator
passphrase += generateWord()
return passphrase
def copyToClipboard(what):
pyperclip.copy(what)
def eraseClipboard(seconds=7):
for i in range(seconds, 0, -1):
print("\r", end="[+] Erasing clipboard in " + str(i) + " seconds...")
sleep(1)
pyperclip.copy("")
print("\n[+] Clipboard erased!")
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser(prog='Diceware Password generator')
parser.add_argument('-o', help='Print passphrase to stdout', action="store_true")
parser.add_argument('-f', metavar="FILE", help='File path to Diceware words')
parser.add_argument('-s', metavar="SEPARATOR", help='Separator')
parser.add_argument('-n', metavar="N_WORDS", help='Number of words')
args = parser.parse_args()
separator = " "
file = "default.txt"
nWords = 5
if args.f: file = args.f
if args.s: separator = args.s
if args.n: nWords = int(args.n)
passw = generatePassphrase(file=file, separator=separator, passPhraseWords=nWords)
if args.o:
print(passw)
copyToClipboard(passw)
eraseClipboard(10)
except KeyboardInterrupt:
pyperclip.copy("")
print("\n[+] Clipboard erased!")