import hashlib def hash_password(password: str) -> str: """ 使用 MD5 对密码进行加密(不推荐用于生产环境) 参数: password (str): 明文密码 返回: str: MD5 加密后的密码(32 位十六进制字符串) """ # 创建 MD5 哈希对象 md5 = hashlib.md5() # 更新哈希对象内容(需将字符串编码为 bytes) md5.update(password.encode('utf-8')) # 获取十六进制表示的哈希值 return md5.hexdigest() def check_password(plain_password: str, hashed_password: str) -> bool: """ 验证密码是否匹配(MD5 版本) 参数: plain_password (str): 明文密码 hashed_password (str): MD5 加密后的密码 返回: bool: 密码是否匹配 """ # 计算明文密码的 MD5 值 plain_password_md5 = hash_password(plain_password) # 比较两个 MD5 值是否相同 return plain_password_md5 == hashed_password