password_needs_rehash
password_needs_rehash(string $hash, string|int|null $algo, array $options = []): bool
密码哈希升级检测:存储的哈希串即自描述元数据($2y$12$ 前缀解析),登录验证通过后按需 rehash 换算法/提 cost;password_hash 族收口第三件。
C C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 从 bcrypt 哈希解析 cost:$2y$12$... -> 12 */
static int bcrypt_cost(const char *hash) {
const char *p = strchr(hash + 1, '$'); /* 算法标识 "2y" 的结尾 */
return p != NULL ? atoi(p + 1) : -1;
}
int main(void) {
const char *hash = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.";
int cost = bcrypt_cost(hash);
printf("%d\n", cost); /* 12 */
printf("%d\n", cost != 12); /* 策略 cost 12:无需 rehash */
printf("%d\n", cost < 13); /* 策略提到 13:需要 rehash */
printf("%d\n", strncmp(hash, "$2y$", 4) != 0); /* 算法不同则为 1 */
return 0;
}
12 0 1 0
C++ C++26
#include <cstdlib>
#include <iostream>
#include <string>
int main() {
std::string hash = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.";
int cost = std::atoi(hash.c_str() + hash.find('$', 1) + 1); // 跳过 "2y"
std::cout << std::boolalpha;
std::cout << cost << "\n";
std::cout << (cost != 12) << "\n"; // 策略 cost 12:无需 rehash
std::cout << (cost < 13) << "\n"; // 策略提到 13:需要 rehash
std::cout << (hash.rfind("$2y$", 0) != 0) << "\n"; // 前缀不符:算法不同
return 0;
}
12 false true false
C# v11.0.100
// 手写:前缀 + cost 比较,与 PHP 逻辑逐字对应
var hash = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.";
var parts = hash.Split('$'); // ["", "2y", "12", "..."]
var cost = int.Parse(parts[2]);
Console.WriteLine(cost);
Console.WriteLine(parts[1] != "2y" || cost != 12); // 策略 cost 12:无需 rehash
Console.WriteLine(parts[1] != "2y" || cost < 13); // 策略提到 13:需要 rehash
Console.WriteLine(parts[1] != "2y"); // 算法不是 bcrypt?
12 False True False
Go v1.27.4
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
hash := "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC."
parts := strings.Split(hash, "$") // ["", "2y", "12", "..."]
cost, _ := strconv.Atoi(parts[2])
fmt.Println(cost)
fmt.Println(parts[1] != "2y" || cost != 12) // 策略 cost 12:无需 rehash
fmt.Println(parts[1] != "2y" || cost < 13) // 策略提到 13:需要 rehash
fmt.Println(parts[1] != "2y") // 算法不是 bcrypt?
}
12 false true false
Java v27.0.1
public class Main {
public static void main(String[] args) {
String hash = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.";
String[] parts = hash.split("\\$"); // ["", "2y", "12", "..."]
int cost = Integer.parseInt(parts[2]);
System.out.println(cost);
System.out.println(!parts[1].equals("2y") || cost != 12); // 策略 cost 12
System.out.println(!parts[1].equals("2y") || cost < 13); // 策略提到 13
System.out.println(!parts[1].equals("2y")); // 算法不同?
}
}
12 false true false
JavaScript v26.13.1
// 其他生态手写同一检查:解析自描述前缀
const hash = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.";
const [, ident, costS] = hash.split("$");
const cost = Number(costS);
console.log(cost);
console.log(ident !== "2y" || cost !== 12); // 策略 cost 12:无需 rehash
console.log(ident !== "2y" || cost < 13); // 策略提到 13:需要 rehash
console.log(ident !== "2y"); // 算法不是 bcrypt?
12 false true false
PHP v8.6.0
// 存储的哈希串即自描述元数据:$2y$12$... = bcrypt、cost 12
$hash = '$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.';
var_dump(password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 12])); // 策略未变:保留
var_dump(password_needs_rehash($hash, PASSWORD_BCRYPT, ['cost' => 13])); // cost 提高:重哈希
var_dump(password_needs_rehash($hash, PASSWORD_ARGON2ID)); // 算法更换:重哈希
bool(false) bool(true) bool(true)
Python v3.15.2
# 手写:解析自描述前缀并与策略比较
hash_ = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC."
_, ident, cost_s, _ = hash_.split("$", 3) # ["", "2y", "12", "..."]
cost = int(cost_s)
print(cost)
print(ident != "2y" or cost != 12) # 策略 cost 12:无需 rehash
print(ident != "2y" or cost < 13) # 策略提到 13:需要 rehash
print(ident != "2y") # 算法不是 bcrypt?
12 False True False
Rust v1.99.1
fn main() {
let hash = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.";
let parts: Vec<&str> = hash.split('$').collect(); // ["", "2y", "12", "..."]
let cost: u32 = parts[2].parse().unwrap();
println!("{}", cost);
println!("{}", parts[1] != "2y" || cost != 12); // 策略 cost 12:无需 rehash
println!("{}", parts[1] != "2y" || cost < 13); // 策略提到 13:需要 rehash
println!("{}", parts[1] != "2y"); // 算法不是 bcrypt?
}
12 false true false
TypeScript v26.13.1
// 运行时同 JS;解构形状保持显式
const hash: string = "$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.";
const [, ident, costS]: string[] = hash.split("$");
const cost: number = Number(costS);
console.log(cost);
console.log(ident !== "2y" || cost !== 12);
console.log(ident !== "2y" || cost < 13);
console.log(ident !== "2y");
12 false true false
语言差异说明
C:手写前缀解析——$2y$12$... 的 cost 在第二、第三个 $ 之间,atoi 取出后与策略比较;这是各语言对照的通用思路。
C++:std::string::rfind(prefix, 0) 判前缀(== 0 即命中),cost 用 atoi 从偏移处解析。
C#:PHP 的 password_needs_rehash 在其他生态通常由框架代劳(如 BCrypt.Net 的 NeedsRehash);手写就是前缀 + cost 比较,与 PHP 逻辑逐字对应。
Go:标准库无 password 族;strings.Split(hash, "$") 后比较 parts[1]/parts[2],生产用 golang.org/x/crypto/bcrypt 的 CompareHashAndPassword 验证 + bcrypt.DefaultCost 比较升级。
Java:标准库无 bcrypt(JDK 只到 PBKDF2);生态用 Spring Security 的 BCryptPasswordEncoder 或 jBCrypt,手写 needs-rehash 即前缀解析。
JavaScript:bcrypt 实现都是 npm 包(bcryptjs 等);password_needs_rehash 的对应物是解析 cost 后与目标 cost 比较,或库自带的 getRounds(hash)(bcryptjs 提供)。
PHP:密码哈希族收口第三件(password_hash/password_verify 已各有单条)。核心教学:存储的哈希串即自描述元数据——$2y$12$... 中 $2y$ 是算法(bcrypt),12 是 cost(2 的幂次代价,每 +1 耗时翻倍),盐已编码进串本身,无需另存。典型迁移流:password_verify 通过后调 password_needs_rehash——为真则用新参数重哈希并替换库里旧串,登录流程无感升级。PASSWORD_DEFAULT 目前仍是 bcrypt,但应显式传目标算法与 options,别依赖默认值漂移。
Python:crypt 模块 3.13 已移除;生态用 bcrypt/passlib 包。bcrypt.checkpw 验证后比较 bcrypt.gensalt(rounds=N) 的轮数或解析出的 cost。
Rust:bcrypt crate 的 verify 只做验证;needs-rehash 手写——解析 cost 与 bcrypt::DEFAULT_COST(当前 12)比较,或库函数 bcrypt::hash_with_salt 重哈希。
TypeScript:运行时同 JS;类型标注让 ident/cost 的解构形状显式。