ckst commited on
Commit
9591d33
·
verified ·
1 Parent(s): 050d600

Create auth.py

Browse files
Files changed (1) hide show
  1. auth.py +34 -0
auth.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from hashlib import sha256
2
+
3
+ # 密码加密函数
4
+ def hash_password(password):
5
+ return sha256(password.encode()).hexdigest()
6
+
7
+ # 模拟数据库操作
8
+ class Database:
9
+ def __init__(self):
10
+ self.users = {}
11
+
12
+ def save_user(self, username, hashed_password):
13
+ self.users[username] = {"password": hashed_password, "is_active": True}
14
+
15
+ def find_user(self, username):
16
+ return self.users.get(username)
17
+
18
+ db = Database()
19
+
20
+ # 注册函数
21
+ def register(username, password):
22
+ hashed_password = hash_password(password)
23
+ db.save_user(username, hashed_password)
24
+ return "注册成功"
25
+
26
+ # 登录函数
27
+ def login(username, password):
28
+ user = db.find_user(username)
29
+ if not user:
30
+ return "用户名或密码错误"
31
+ if user["password"] == hash_password(password):
32
+ return "登录成功"
33
+ else:
34
+ return "用户名或密码错误"