验证密码是否匹配已存储的哈希值。 支持透明升级:如果存储的是旧版 MD5 哈希,也能正确验证。 :param row_password: 明文密码 :param hashed_password: 数据库中存储的密码哈希 :return: 是否匹配
(row_password, hashed_password)
| 51 | |
| 52 | |
| 53 | def password_verify(row_password, hashed_password): |
| 54 | """ |
| 55 | 验证密码是否匹配已存储的哈希值。 |
| 56 | 支持透明升级:如果存储的是旧版 MD5 哈希,也能正确验证。 |
| 57 | :param row_password: 明文密码 |
| 58 | :param hashed_password: 数据库中存储的密码哈希 |
| 59 | :return: 是否匹配 |
| 60 | """ |
| 61 | # First try Django's built-in check (PBKDF2, bcrypt, argon2, etc.) |
| 62 | if check_password(row_password, hashed_password): |
| 63 | return True |
| 64 | # Fall back to legacy MD5 comparison for not-yet-migrated hashes |
| 65 | if _is_legacy_md5_hash(hashed_password): |
| 66 | return _legacy_md5_hash(row_password) == hashed_password |
| 67 | return False |
| 68 | |
| 69 | |
| 70 | def _is_legacy_md5_hash(hashed_password): |
no test coverage detected