用Python写一个注册账号密码和存储账号密码以及校验账号密码是否和存储的账号密码相同的代码
时间: 2024-05-08 07:17:07 浏览: 13
以下是使用Python的示例代码:
```python
import hashlib
# 用字典存储账号密码
users = {}
# 注册账号密码
def register(username, password):
if username in users:
print("Error: Username already exists")
else:
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
users[username] = hashed_password
print("Success: Account created")
# 校验账号密码
def login(username, password):
if username not in users:
print("Error: Username does not exist")
else:
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
if users[username] == hashed_password:
print("Success: Login successful")
else:
print("Error: Incorrect password")
# 测试注册和登录功能
register("user1", "password1")
login("user1", "password1")
login("user1", "password2")
register("user1", "password2")
```