-
Notifications
You must be signed in to change notification settings - Fork 0
/
passgen.py
executable file
·65 lines (48 loc) · 1.76 KB
/
passgen.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""xkcd-ish Password Generator Function. https://xkcd.com/936/
Generates an xkcd-ish style of password.
Typical usage example:
pass = passgen() # customized xkcd style
pass = passgen(4, " ", False, False) # xkcd style
Author: Abdul Rahman Dabbour
License: MIT
"""
import secrets
import string
import words
def passgen(
word_count: int = 2,
delimiter: str = "-",
contains_number: bool = True,
title_case: bool = True,
) -> str:
"""xkcd-ish Password Generator Function. https://xkcd.com/936/
Args:
word_count (int): Number of words must be 1 > word_count > 9.
delimiter (str): Delimiter between words; must be empty space or in
`string.punctuation`.
contains_number (bool): Adds a number in the range [0, 100) if true.
title_case (bool): Makes each word in the password Title Case.
Returns:
The generated password
"""
assert isinstance(word_count, int)
assert isinstance(delimiter, str)
assert isinstance(contains_number, bool)
assert isinstance(title_case, bool)
if word_count < 1 or word_count > 9:
raise ValueError("Number of words in range (1, 10)!")
if len(delimiter) != 1:
raise ValueError("Delimiter must be a single character!")
if delimiter not in string.punctuation + " ":
raise ValueError(
"Delimiter must be empty space or `string.punctuation`!"
)
base_pass = [secrets.choice(words.WORDS) for _ in range(word_count)]
if title_case:
base_pass = [x.title() for x in base_pass]
if contains_number:
pos = round(word_count / 2.0)
base_pass[pos:pos] = [str(secrets.randbelow(100))]
return delimiter.join(base_pass)