-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassGenSecure.py
More file actions
160 lines (138 loc) · 6.27 KB
/
PassGenSecure.py
File metadata and controls
160 lines (138 loc) · 6.27 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import random
import string
import sqlite3
import getpass
import tldextract
# Valid top-level domains
VALID_DOMAINS = ['.com', '.net', '.org', '.gov', '.edu']
# Connect to database
conn = sqlite3.connect('passwords.db')
c = conn.cursor()
# Create table if not exists
c.execute('''CREATE TABLE IF NOT EXISTS passwords
(website text, username text, password text)''')
# Password Generator
def password():
while True:
newSite = input("Please type a website you would like to generate a password for. "
"If you no longer want to continue please type 'Done': ")
if newSite.lower() == 'done':
break
# Validate website
domain = tldextract.extract(newSite).suffix
if domain not in VALID_DOMAINS:
print("Invalid website. Please enter a valid website with a .com, .net, .org, .gov, or .edu domain.")
continue
newPassword = eval(input("Please type the number of characters you would like in your password: "))
# Expand character set to include lowercase letters and special characters
randomPassword = ''.join(
random.choices(string.ascii_letters + string.digits + string.punctuation, k=newPassword))
print("Your new password is:", randomPassword)
print("Would you like to store it?")
store = input("(Type Y or N): ")
if store.lower() == "y":
username = input("Please enter a username for this website: ")
# Store password securely using getpass library
password = getpass.getpass(prompt="Please enter the password again to confirm: ")
c.execute("INSERT INTO passwords (website, username, password) VALUES (?, ?, ?)",
(newSite, username, password))
conn.commit()
print("Now stored.")
if store.lower() == "n":
print("Now discarded.")
# Username Generator
def username():
while True:
newUsername = input("Please type a website you would like to generate a username for. "
"If you no longer want to continue please type 'Done': ")
if newUsername.lower() == "done":
break
# Validate website
domain = tldextract.extract(newUsername).suffix
if domain not in VALID_DOMAINS:
print("Invalid website. Please enter a valid website with a .com, .net, .org, .gov, or .edu domain.")
continue
newUser = eval(input("Please type the number of characters you would like in your username: "))
# Expand character set to include lowercase letters and other options
randomUser = ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=newUser))
print("Your new username is:", randomUser)
print("Would you like to store it?")
store = input("(Type Y or N): ")
if store.lower() == "y":
c.execute("INSERT INTO passwords (website, username) VALUES (?, ?)", (newUsername, randomUser))
conn.commit()
print("Now stored.")
if store.lower() == "n":
print("Now discarded.")
# View stored passwords and usernames
def view():
c.execute("SELECT * FROM passwords")
print("Website\t\tUsername\t\tPassword")
print("-----------------------------------------------")
for row in c.fetchall():
print(row[0] + "\t\t" + row[1] + "\t\t" + row[2])
# Delete username, password, or both
def delete_data():
while True:
website = input("Enter the website whose data you want to delete (Type 'Done' to exit): ")
if website.lower() == 'done':
break
username = input("Enter the username for the website: ")
password = getpass.getpass(prompt="Enter the password for the website: ")
while True:
delete_type = input("What would you like to delete? Type 'U' for username, 'P' for password, or 'B' for "
"both: ")
if delete_type.lower() == 'u':
confirm = input("Are you sure you want to delete the username for {}? (Y/N): ".format(website))
if confirm.lower() == 'y':
c.execute("UPDATE passwords SET username = NULL WHERE website = ? AND username = ?",
(website, username))
conn.commit()
print("Username for {} deleted.".format(website))
else:
print("Data not deleted.")
break
elif delete_type.lower() == 'p':
confirm = input("Are you sure you want to delete the password for {}? (Y/N): ".format(website))
if confirm.lower() == 'y':
c.execute("UPDATE passwords SET password = NULL WHERE website = ? AND username = ?",
(website, username))
conn.commit()
print("Password for {} deleted.".format(website))
else:
print("Data not deleted.")
break
elif delete_type.lower() == 'b':
confirm = input(
"Are you sure you want to delete the username and password for {}? (Y/N): ".format(website))
if confirm.lower() == 'y':
c.execute("DELETE FROM passwords WHERE website = ? AND username = ?", (website, username))
conn.commit()
print("Username and password for {} deleted.".format(website))
else:
print("Data not deleted.")
break
else:
print("Invalid option. Please try again.")
# Main loop
while True:
print("\nWhat would you like to do today?")
print("1. Generate a new password")
print("2. Generate a new username")
print("3. View stored passwords and usernames")
print("4. Delete username, password, or both")
print("5. Exit program")
choice = input("\nEnter the number of your choice: ")
if choice == "1":
password()
elif choice == "2":
username()
elif choice == "3":
view()
elif choice == "4":
delete_data()
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 5.")