Files
flask_rulebase_serve/app/utils/password_hasher.py
2026-04-22 13:35:40 +08:00

38 lines
1006 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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