password_get_info
password_get_info(string $hash): array
解析密码哈希的前缀元信息(算法 ID/algoName/cost 等),不校验哈希本身;password_hash/verify/needs_rehash/get_info 四件套就此收口。
C C23
#include <stdio.h>
#include <string.h>
/* password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串 */
int main(void) {
const char *fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
/* 布局:$2y$<两位 cost>$<22 字符 salt><31 字符 digest> */
if (strncmp(fixed, "$2", 2) == 0 && fixed[3] == '$') {
printf("algo name: bcrypt\n");
printf("cost: %.2s\n", fixed + 4);
printf("hash length: %zu\n", strlen(fixed));
}
const char *md5 = "5f4dcc3b5aa765d61d8327deb882cf99";
printf("unknown: %s\n", strncmp(md5, "$2", 2) != 0 ? "true" : "false");
return 0;
}
algo name: bcrypt cost: 12 hash length: 60 unknown: true
C++ C++26
#include <cstring>
#include <iostream>
#include <regex>
#include <string>
int main() {
// password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
const std::string fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
static const std::regex re{R"(^\$2[aby]\$(\d{2})\$([./A-Za-z0-9]{53})$)"};
std::smatch m;
std::cout << "matched: " << std::boolalpha << std::regex_match(fixed, m, re) << "\n";
std::cout << "algo name: bcrypt\n";
std::cout << "cost: " << std::stoi(m[1]) << "\n";
std::cout << "hash length: " << fixed.size() << "\n";
std::cout << "unknown: " << !std::regex_match(std::string("5f4dcc3b5aa765d61d8327deb882cf99"), re) << "\n";
return 0;
}
matched: true algo name: bcrypt cost: 12 hash length: 60 unknown: true
C# v11.0.100
// password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
var hashStr = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
var re = new System.Text.RegularExpressions.Regex(@"^\$2[aby]\$(\d{2})\$([./A-Za-z0-9]{53})$");
var m = re.Match(hashStr);
Console.WriteLine("matched: " + m.Success);
Console.WriteLine("algo name: bcrypt");
Console.WriteLine("cost: " + int.Parse(m.Groups[1].Value));
Console.WriteLine("hash length: " + hashStr.Length);
Console.WriteLine("unknown: " + !re.IsMatch("5f4dcc3b5aa765d61d8327deb882cf99"));
matched: True algo name: bcrypt cost: 12 hash length: 60 unknown: True
Go v1.27.4
package main
import (
"fmt"
"regexp"
"strconv"
)
func main() {
// password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
fixed := "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS"
re := regexp.MustCompile(`^\$2[aby]\$(\d{2})\$([./A-Za-z0-9]{53})$`)
m := re.FindStringSubmatch(fixed)
cost, _ := strconv.Atoi(m[1])
fmt.Println("algo name: bcrypt")
fmt.Println("cost:", cost)
fmt.Println("hash length:", len(fixed))
fmt.Println("unknown:", re.FindStringSubmatch("5f4dcc3b5aa765d61d8327deb882cf99") == nil)
}
algo name: bcrypt cost: 12 hash length: 60 unknown: true
Java v27.0.1
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
String fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
var re = Pattern.compile("^\\$2[aby]\\$(\\d{2})\\$([./A-Za-z0-9]{53})$");
var m = re.matcher(fixed);
System.out.println("matched: " + m.matches());
System.out.println("algo name: bcrypt");
System.out.println("cost: " + Integer.parseInt(m.group(1)));
System.out.println("hash length: " + fixed.length());
System.out.println("unknown: " + !re.matcher("5f4dcc3b5aa765d61d8327deb882cf99").matches());
}
}
matched: true algo name: bcrypt cost: 12 hash length: 60 unknown: true
JavaScript v26.13.1
// password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
const fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
const re = /^\$2[aby]\$(\d{2})\$([./A-Za-z0-9]{53})$/;
const m = re.exec(fixed);
console.log("matched:", m !== null);
console.log("algo name: bcrypt");
console.log("cost:", Number(m[1]));
console.log("hash length:", fixed.length);
console.log("unknown:", re.exec("5f4dcc3b5aa765d61d8327deb882cf99") === null);
matched: true algo name: bcrypt cost: 12 hash length: 60 unknown: true
PHP v8.6.0
// password_get_info:把哈希串解析成算法名 + 参数
$hash = password_hash('secret', PASSWORD_BCRYPT, ['cost' => 12]);
$info = password_get_info($hash);
printf("algo=%d algoName=%s\n", $info['algo'], $info['algoName']);
printf("options cost=%d\n", $info['options']['cost']);
// 哈希串本身就内嵌同一信息:$2y$<cost>$<22 字符 salt><31 字符 digest>
printf("prefix: %s\n", substr($hash, 0, 7)); // $2y$12$——可手写解析
var_dump(strlen($hash));
// 非密码哈希(如普通 md5)被识别为 unknown
$info2 = password_get_info('5f4dcc3b5aa765d61d8327deb882cf99');
printf("unknown algo=%d algoName=%s\n", $info2['algo'], $info2['algoName']);
// 固定哈希供跨语言解析演示(各语言解析同一根字符串)
$fixed = '$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS';
printf("fixed: algoName=%s cost=%d len=%d\n", password_get_info($fixed)['algoName'], password_get_info($fixed)['options']['cost'], strlen($fixed));
algo=2 algoName=bcrypt options cost=12 prefix: $2y$12$ int(60) unknown algo=0 algoName=unknown fixed: algoName=bcrypt cost=12 len=60
Python v3.15.2
import re
# password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS"
m = re.fullmatch(r"\$2[aby]\$(\d{2})\$([./A-Za-z0-9]{53})", fixed)
print("algo name: bcrypt")
print("cost:", int(m.group(1)))
print("hash length:", len(fixed))
# 未知字符串(如普通 md5)匹配不到——算法为 unknown
print("unknown:", re.fullmatch(r"\$2[aby]\$(\d{2})\$([./A-Za-z0-9]{53})", "5f4dcc3b5aa765d61d8327deb882cf99") is None)
algo name: bcrypt cost: 12 hash length: 60 unknown: True
Rust v1.99.1
fn main() {
// password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
// stable std 无正则;手写解析 $2y$<cost>$ 布局
let fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
let matched = fixed.starts_with('$') && fixed[3..4].chars().next() == Some('$');
println!("matched: {}", matched);
println!("algo name: bcrypt"); // $2a/$2b/$2y 都是 bcrypt 变体
println!("cost: {}", &fixed[4..6].parse::<u32>().unwrap());
println!("hash length: {}", fixed.len());
let md5 = "5f4dcc3b5aa765d61d8327deb882cf99";
println!("unknown: {}", !md5.starts_with("$2"));
}
matched: true algo name: bcrypt cost: 12 hash length: 60 unknown: true
TypeScript v26.13.1
// password_get_info 解析 bcrypt 哈希的前缀——这里用同一根字符串
const fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
const re = /^\$2[aby]\$(\d{2})\$([./A-Za-z0-9]{53})$/;
const m = re.exec(fixed);
console.log("matched:", m !== null);
console.log("algo name: bcrypt");
console.log("cost:", Number(m[1]));
console.log("hash length:", fixed.length);
console.log("unknown:", re.exec("5f4dcc3b5aa765d61d8327deb882cf99") === null);
matched: true algo name: bcrypt cost: 12 hash length: 60 unknown: true
语言差异说明
C:无密码哈希生态(需自带 bcrypt/argon2 库实现)——识别哈希只能手写前缀解析(strncmp "$2" 布局拆分)。
C++:同 C——手写或引入 libbcrypt/argon2;示例用 std::regex 全匹配拆 cost。
C#:.NET 无内置密码哈希(bcrypt/argon2 都是 NuGet 包)——前缀解析用 Regex;注意 fixed 是 C# 关键字,变量需换名。
Go:golang.org/x/crypto/bcrypt 是事实标准——bcrypt.Cost(hash) 直接读 cost;识别哈希用正则拆 $2y$ 前缀。
Java:无内置密码哈希——jBCrypt(BCrypt.hashpw/checkpw)是惯例;前缀解析用 Pattern。
JavaScript:Node 无密码哈希内建——bcrypt/argon2 npm 包是惯例;前缀解析用正则。
PHP:password_get_info 只解析前缀不校验哈希——返回 algo(整数算法编号,bcrypt 实测为 2)/algoName('bcrypt')/options(cost);未知格式返回 algo=0/algoName=unknown;与 password_hash/password_verify/password_needs_rehash 四件套闭环——needs_rehash 正是靠 get_info 的 cost 判断要不要重哈希;注意 PASSWORD_BCRYPT/PASSWORD_DEFAULT 常量本身是字符串 '2y'(本机 8.6 实测),与 get_info 的整数 algo 不是同一记法。
Python:stdlib 无密码哈希——bcrypt/argon2-cffi 包是惯例;前缀解析用 re;passlib 库还能反向识别所有格式。
Rust:bcrypt crate(已在依赖清单)或 argon2 crate;stable std 无正则——手写拆分 $2y$<cost>$ 布局。
TypeScript:同 JavaScript——bcrypt/argon2 npm 包。