-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_user.py
More file actions
24 lines (21 loc) · 751 Bytes
/
add_user.py
File metadata and controls
24 lines (21 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sqlite3
import hashlib
def add_user(username, password):
"""
Add a new user to the users.db database.
Password is hashed using SHA-256 before storing.
"""
hashed_password = hashlib.sha256(password.encode()).hexdigest()
conn = sqlite3.connect('users.db')
c = conn.cursor()
try:
c.execute("INSERT INTO users (username, password_hash) VALUES (?, ?)", (username, hashed_password))
conn.commit()
print(f"User '{username}' added successfully.")
except sqlite3.IntegrityError:
print(f"User '{username}' already exists.")
conn.close()
if __name__ == "__main__":
username = input("Enter username: ")
password = input("Enter password: ")
add_user(username, password)