forked from React-Group/interstellar_ai
wha
This commit is contained in:
parent
1ee7d55e49
commit
4a183c0c89
2 changed files with 50 additions and 0 deletions
41
py/db.py
41
py/db.py
|
@ -0,0 +1,41 @@
|
|||
import json
|
||||
import hashlib
|
||||
|
||||
|
||||
class DB:
|
||||
def __init__(self):
|
||||
self.database = {}
|
||||
|
||||
def _hash_password(self, password: str) -> str:
|
||||
salt = "your_secret_salt"
|
||||
hashed_password = hashlib.sha256((password + salt).encode()).hexdigest()
|
||||
return hashed_password
|
||||
|
||||
def add_user(self, username: str, password: str) -> None:
|
||||
hashed_password = self._hash_password(password)
|
||||
user_data = {"hashed_password": hashed_password}
|
||||
self.database[username] = user_data
|
||||
|
||||
def update_password(self, username: str, old_password: str, new_password: str) -> bool:
|
||||
if not self.check_credentials(username, old_password):
|
||||
return False
|
||||
|
||||
hashed_new_password = self._hash_password(new_password)
|
||||
self.database[username].update({"hashed_password": hashed_new_password})
|
||||
return True
|
||||
|
||||
def check_credentials(self, username: str, password: str) -> bool:
|
||||
if username not in self.database:
|
||||
return False
|
||||
|
||||
stored_hashed_password = self.database[username]["hashed_password"]
|
||||
entered_hashed_password = self._hash_password(password)
|
||||
return stored_hashed_password == entered_hashed_password
|
||||
|
||||
def get_additional_info(self, username: str, password: str) -> dict | None:
|
||||
if not self.check_credentials(username, password):
|
||||
return None
|
||||
|
||||
send_back = self.database[username]
|
||||
del send_back['hashed_password']
|
||||
return send_back
|
Loading…
Add table
Add a link
Reference in a new issue