-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_password.py
executable file
·60 lines (47 loc) · 1.53 KB
/
gen_password.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
#!/usr/bin/env python3
import argparse
import glob
import logging
import os
import secrets
import string
logger = logging.getLogger("gen_password")
sources = []
alphabet = set(string.ascii_lowercase) - set("lmn")
def load_data():
# already loaded
if sources:
return
here = os.path.dirname(os.path.abspath(__file__))
files = glob.glob(here + "/password_data/*.txt")
for path in sorted(files):
logger.debug("Loading password data from %s", path)
cur = []
with open(path) as f:
data = f.read().splitlines()
for w in data:
if all(c in alphabet for c in w) and 3 <= len(w) <= 8:
cur.append(w)
sources.append(cur)
def gen_password(method="last"):
load_data()
if method == "last":
parts = list(map(secrets.choice, [sources[-1]] * 5))
elif method == "all":
parts = list(map(secrets.choice, sources))
else:
raise ValueError("Unsupported method")
return "-".join(parts)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"number", help="Number of passwords to generate", type=int, default=1, nargs="?"
)
parser.add_argument("--verbose", "-v", help="Verbose output", action="store_true")
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
format="%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
)
for _ in range(args.number):
print(gen_password())