Create auth.py
Browse files12345678910111213141516171819202122232425262728293031323334from hashlib import sha256
# 密码加密函数def hash_password(password): return sha256(password.encode()).hexdigest()
# 模拟数据库操作class Database: def __init__(self): self.users = {}
def save_user(self, username, hashed_password): self.users[username] = {"password": hashed_password, "is_active": True}
def find_user(self, username): return self.users.get(username)
db = Database()
# 注册函数def register(username, password): hashed_password = hash_password(password) db.save_user(username, hashed_password) return "注册成功"
# 登录函数def login(username, password): user = db.find_user(username) if not user: return "用户名或密码错误" if user["password"] == hash_password(password): return "登录成功" else: return "用户名或密码错误"auth.pyAdded+34