-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
46 lines (37 loc) · 1 KB
/
Copy pathdatabase.py
File metadata and controls
46 lines (37 loc) · 1 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
import sqlite3
import bcrypt
DATABASE = "database/passwords.db"
def create_table():
conn = sqlite3.connect(DATABASE)
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS passwords(
id INTEGER PRIMARY KEY AUTOINCREMENT,
password_hash TEXT
)
""")
conn.commit()
conn.close()
def password_exists(password):
conn = sqlite3.connect(DATABASE)
cur = conn.cursor()
cur.execute("SELECT password_hash FROM passwords")
rows = cur.fetchall()
conn.close()
for row in rows:
if bcrypt.checkpw(password.encode(), row[0].encode()):
return True
return False
def save_password(password):
conn = sqlite3.connect(DATABASE)
cur = conn.cursor()
hashed = bcrypt.hashpw(
password.encode(),
bcrypt.gensalt()
).decode()
cur.execute(
"INSERT INTO passwords(password_hash) VALUES(?)",
(hashed,)
)
conn.commit()
conn.close()