-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_generator.py
More file actions
80 lines (63 loc) · 2.16 KB
/
password_generator.py
File metadata and controls
80 lines (63 loc) · 2.16 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
import random
import string
import sqlite3
def generate_db():
conn = sqlite3.connect("db\passwords.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE passwords (password text)")
conn.commit()
conn.close()
def generate_password(length):
# generate a random password with the specified length
password = ''.join(random.choices(string.ascii_letters + string.digits, k=length))
return password
def insert_password(password):
# insert the password into the database
conn = sqlite3.connect("db\passwords.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO passwords (password) VALUES (?)", (password,))
conn.commit()
conn.close()
def check_password(password):
# check if the password already exists in the database
conn = sqlite3.connect("db\passwords.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM passwords WHERE password=?", (password,))
result = cursor.fetchone()
conn.close()
if result:
# password already exists in the database
return True
else:
# password does not exist in the database
return False
def main():
# get the desired password length from the user
length = int(input("Enter the desired password length: "))
# generate a random password
password = generate_password(length)
# check if the password already exists in the database
while check_password(password):
# if the password already exists, generate a new one
password = generate_password(length)
# password is unique, insert it into the database
insert_password(password)
# output the generated password to the user
print("Your generated password is:", password)
def table_exists(table_name):
conn = sqlite3.connect("db\passwords.db")
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,))
result = cursor.fetchone()
conn.close()
if result:
return True
else:
return False
if __name__ == "__main__":
if table_exists("passwords"):
print()
else:
generate_db()
main()
# program ends here 🖤