-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
62 lines (49 loc) · 1.78 KB
/
generator.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
"""generator.py
The generator part of the password generator. Overall it would
work much easier with regex (https://regexr.com/ saves lives).
"""
from random import randint, sample, shuffle
import string
import re
alphabet = string.ascii_letters
digits = string.digits
weakpw = string.ascii_letters + string.digits
symbols = string.punctuation
no_whitespace = string.printable.strip()
def pwdgen(strength: str) -> str:
"""Sloppy password generator that creates passwords based
on randomly thought out criteria.
Parameters:
strength (str): Expects "strong" or "weak"
Returns:
pwd (str): Returns the password
"""
strength = strength.lower()
pwd = "00"
if strength == "strong":
while len(set(pwd.lower())) != len(pwd.lower()):
pwd = mandatory_strong()
pwd += "".join(sample(no_whitespace, randint(4, 8)))
pwd = list(pwd)
shuffle(pwd)
return "".join(pwd)
elif strength == "weak":
while len(re.findall(r"(?=[a-zA-Z0-9])([a-zA-Z0-9])\1{1,}", pwd.lower())) != 0:
pwd = ""
pwd += "".join(sample(weakpw, randint(6, 8)))
return pwd
def mandatory_strong() -> str:
"""Small utility to:
- reset the password in case there are duplicates caught by the
length-comparison in the while loop of pwdgen()
- fill up the string with the mandatory elements set for strong
passwords (symbol, upper- and lowercase letter, digit)
Parameters:
None
Returns:
pwd (str): Returns pwd as str to use in pwdgen()
"""
pwd = ""
one_of_each = "".join(sample(alphabet[:26], 1)) + "".join(sample(alphabet[26:], 1)) + "".join(sample(digits, 1)) + "".join(sample(symbols, 1))
pwd += one_of_each
return pwd