|
from 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 "用户名或密码错误" |