interstellar_ai/py/db.py
2024-09-20 15:26:18 +02:00

57 lines
1.7 KiB
Python

import json
import hashlib
class DB:
def __init__(self):
self.database = {}
@staticmethod
def hash_password(password):
salt = "your_secret_salt"
hashed_password = hashlib.sha256((password + salt).encode()).hexdigest()
return hashed_password
def add_user(self, data):
username = data.get['username']
password = data.get['password']
hashed_password = self.hash_password(password)
user_data = {"hashed_password": hashed_password}
self.database[username] = user_data
def change_data(self, data):
username = data.get['username']
data = data.get['data']
if not self.check_credentials(data):
return False
self.database[username]['data'] = data
return True
def update_password(self, data):
username = data.get['username']
new_password = data.get['new_password']
if not self.check_credentials(data):
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, data):
username = data.get['username']
password = data.get['password']
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_data(self, data):
username = data.get['username']
if not self.check_credentials(data):
return None
send_back = self.database[username].get['data']
return send_back