今日错误

今天才发现自己总是不知不觉的犯了很多的错误。

 

自己把自己的部分的信息都写错了,以后我如果参加考核,岂不是很吃亏。

 

我已经为自己的错误付出了很多的代价,要警醒啊

### 实现带有错误次数限制的用户登录功能 为了实现安全可靠的用户登录验证逻辑,可以按照如下方式编写 Python 代码: ```python import hashlib from datetime import datetime, timedelta from typing import Dict class UserLoginSystem: def __init__(self): # 存储用户名到哈希后的密码映射关系 self.user_passwords: Dict[str, str] = {} # 记录用户的尝试次数和最后一次尝试时间 self.login_attempts: Dict[str, int] = {} self.last_attempt_time: Dict[str, datetime] = {} @staticmethod def hash_password(password: str) -> str: """使用 SHA-256 对密码进行不可逆加密""" sha_signature = hashlib.sha256(password.encode()).hexdigest() return sha_signature def register_user(self, username: str, password: str): hashed_pwd = self.hash_password(password) self.user_passwords[username] = hashed_pwd print(f"User {username} registered.") def login(self, username: str, input_password: str) -> bool: current_time = datetime.now() if username not in self.user_passwords: print("Error: Username does not exist.") return False # 检查是否被锁定 last_time = self.last_attempt_time.get(username, None) lockout_duration = timedelta(days=1) if (last_time and 'username' in self.login_attempts and self.login_attempts['username'] >= 5 and current_time - last_time < lockout_duration): remaining_lock_time = (lockout_duration - (current_time - last_time)).seconds // 60 + 1 print(f"Account locked. Please try again after {remaining_lock_time} minutes.") return False stored_hashed_password = self.user_passwords[username] if self.hash_password(input_password) != stored_hashed_password: attempts = self.login_attempts.setdefault(username, 0) + 1 self.login_attempts[username] = attempts if attempts >= 5: self.last_attempt_time[username] = current_time print(f"Incorrect password attempt #{attempts}. Try again.") return False else: # 登录成功则清除计数器 del self.login_attempts[username] print("Login successful!") return True if __name__ == "__main__": system = UserLoginSystem() # 注册新用户(仅用于测试) system.register_user('testuser', 'securepassword') while True: user_input_username = input("Enter your username:") user_input_password = input("Enter your password:") result = system.login(user_input_username, user_input_password) if result or ('testuser' in system.login_attempts and system.login_attempts['testuser'] >= 5): break ``` 上述代码实现了基本的身份认证流程,并加入了针对暴力破解攻击的安全措施[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值