请查看CHANGELOG.md获取更新信息
This commit is contained in:
1 parent
bf772046cc
commit
9bb6e6a41e
18 files changed
+879
-105
No files matched your search
+40
-4
@@ -24,22 +24,58 @@ function require_login(string $loginPage = 'login.php'): void
|
||||
}
|
||||
}
|
||||
|
||||
/** 计算需求规定的密码散列:先 base64 再 md5 */
|
||||
/** 密码散列:使用 PHP 内置强散列(bcrypt/argon2),自动加盐与迭代 */
|
||||
function hp_password_hash(string $plain): string
|
||||
{
|
||||
return password_hash($plain, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
/** 旧版散列(md5(base64()))——仅用于兼容迁移,禁止用于新写入 */
|
||||
function hp_password_hash_legacy(string $plain): string
|
||||
{
|
||||
return md5(base64_encode($plain));
|
||||
}
|
||||
|
||||
/** 验证用户 */
|
||||
/** 验证用户(旧版散列验证通过后自动升级为强散列) */
|
||||
function hp_verify_user(string $username, string $plain): bool
|
||||
{
|
||||
$st = db()->prepare('SELECT password_hash FROM users WHERE username = ? LIMIT 1');
|
||||
$st->execute([$username]);
|
||||
$row = $st->fetch();
|
||||
if (!$row) {
|
||||
// 用户不存在也执行一次等价开销,削弱用户名枚举的时序差异
|
||||
password_verify($plain, '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi');
|
||||
return false;
|
||||
}
|
||||
$stored = (string) $row['password_hash'];
|
||||
|
||||
// 旧库:32 位十六进制 md5 → 校验通过后透明升级
|
||||
if (preg_match('/^[0-9a-f]{32}$/i', $stored)) {
|
||||
if (!hash_equals($stored, hp_password_hash_legacy($plain))) {
|
||||
return false;
|
||||
}
|
||||
$upd = db()->prepare('UPDATE users SET password_hash = ? WHERE username = ?');
|
||||
$upd->execute([hp_password_hash($plain), $username]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return password_verify($plain, $stored);
|
||||
}
|
||||
|
||||
/** 指定用户(默认当前登录用户)是否必须修改密码(初始/重置密码未改时为 true) */
|
||||
function hp_password_needs_change(?string $username = null): bool
|
||||
{
|
||||
$username = ($username !== null) ? $username : (string) ($_SESSION['admin'] ?? '');
|
||||
if ($username === '') {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
$st = db()->prepare('SELECT must_change_pwd FROM users WHERE username = ? LIMIT 1');
|
||||
$st->execute([$username]);
|
||||
return ((int) $st->fetchColumn()) === 1;
|
||||
} catch (Throwable $e) {
|
||||
return false;
|
||||
}
|
||||
return hash_equals($row['password_hash'], hp_password_hash($plain));
|
||||
}
|
||||
|
||||
/** 执行登录(成功返回 true;成功/失败均写入登录日志与设备信息) */
|
||||
@@ -73,7 +109,7 @@ function hp_change_password(string $oldPlain, string $newPlain): array
|
||||
if (strlen($newPlain) < 6) {
|
||||
return [false, '新密码至少 6 位'];
|
||||
}
|
||||
$st = db()->prepare('UPDATE users SET password_hash = ? WHERE username = ?');
|
||||
$st = db()->prepare('UPDATE users SET password_hash = ?, must_change_pwd = 0 WHERE username = ?');
|
||||
$st->execute([hp_password_hash($newPlain), (string) $_SESSION['admin']]);
|
||||
return [true, '密码修改成功'];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user