-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordGenerator.py
53 lines (44 loc) · 1.75 KB
/
PasswordGenerator.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
import random as rand
import string as str
import re
# generate random 8 char long PW with at least 1 symbol, 1 number, 1 uppercase, and 1 lowercase
def generatePassword(keyword=""):
lower = str.ascii_lowercase
upper = str.ascii_uppercase
sym = str.punctuation
num = str.digits
possibleChars = lower + upper + sym + num
# add chars to make it 8 chars long if len(keyword) < 8
if (len(keyword < 8)):
keyword = addChars(keyword)
# Convert keyword to list of chars, then add or replace chars
keywordChars = [char for char in keyword]
charsToReplace = rand.sample(range(0,len(keyword)), k=4)
if (re.search('[!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~]', keyword) == None):
keywordChars[charsToReplace[0]] = rand.choice(sym)
if (re.search('[0-9]', keyword) == None):
keywordChars[charsToReplace[1]] = rand.choice(num)
if (re.search('[a-z]', keyword) == None):
keywordChars[charsToReplace[2]] = rand.choice(lower)
if (re.search('[A-Z]', keyword) == None):
keywordChars[charsToReplace[3]] = rand.choice(upper)
# Re-convert to string
keyword = "".join(keywordChars)
print(keyword)
def addChars(keyword):
return keyword+'xxxx'
# Console
print("Select password length: ")
# TODO: add error handling to accept only integers
length = int(input())
keyword = input("Please enter the keyword to base password on: ")
if len(keyword) < length or len(keyword) < 8:
if (length > 8):
# TODO: add characters to make it 8 chars long
print("Error: Keyword must be" + length + "characters long")
else:
print("Error: Keyword must be 8 characters long")
print("Your password is " + addChars(keyword))
# test: 10 tries
for _ in range(10):
generatePassword(keyword)