File size: 881 Bytes
9591d33 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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 "用户名或密码错误" |