import hashlib import json class DB: def __init__(self): self.database = {} self.emails = [] def ensure_username(self, data): if hasattr(data, 'username'): return data.get['username'] elif hasattr(data, 'email'): return self.emails[data.get['username']] @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'] email = data.get['email'] hashed_password = self.hash_password(password) user_data = {"hashed_password": hashed_password} if username not in self.database: self.database[username] = user_data self.emails[email] = username return True return False def delete_user(self, data): username = self.ensure_username(data) data = data.get['data'] if not self.check_credentials(data): return False del self.database[username] for i in self.emails: if i == username: del i self.save_database() return True def change_data(self, data): username = self.ensure_username(data) data = data.get['data'] if not self.check_credentials(data): return False self.database[username]['data'] = data self.save_database() return True def update_password(self, data): username = self.ensure_username(data) 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}) self.save_database() return True def check_credentials(self, data): username = self.ensure_username(data) 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 = self.ensure_username(data) if not self.check_credentials(data): return None send_back = self.database[username].get['data'] return send_back def save_database(self): with open("database.json", 'w') as file: json.dump(self.database, file) def load_database(self): try: with open("database.json", 'r') as file: self.database = json.load(file) except FileNotFoundError: pass