password_get_info
password_get_info(string $hash): array
Parses a password hash's prefix metadata (algo ID/algoName/cost...) without verifying the hash; this completes the password_hash/verify/needs_rehash/get_info quartet.
C C23
#include <stdio.h>
#include <string.h>
/* password_get_info parses a bcrypt hash's PREFIX - same string here */
int main(void) {
const char *fixed = "$2y$12$hyJsRWpKtjrCLe/6WWwdiuJu1PqjnlBpU5q64zMTbBfLgFCehiVAS";
/* Layout: $2y$<2-digit cost>$<22-char salt><31-char 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 parses a bcrypt hash's PREFIX - same string here
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 parses a bcrypt hash's PREFIX - same string here
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 parses a bcrypt hash's PREFIX - same string here
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 parses a bcrypt hash's PREFIX - same string here
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 parses a bcrypt hash's PREFIX - same string here
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: parse a hash string into algo name + options
$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']);
// The hash itself embeds the same data: $2y$<cost>$<22-char salt><31-char digest>
printf("prefix: %s\n", substr($hash, 0, 7)); // $2y$12$ - hand-parseable
var_dump(strlen($hash));
// A non-password hash (e.g. plain md5) is recognized as unknown
$info2 = password_get_info('5f4dcc3b5aa765d61d8327deb882cf99');
printf("unknown algo=%d algoName=%s\n", $info2['algo'], $info2['algoName']);
// Fixed hash for cross-language parsing demos (parse the SAME string everywhere)
$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 parses a bcrypt hash's PREFIX - parse the same string here
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))
# An unknown string (e.g. plain md5) matches nothing - unknown algo
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 parses a bcrypt hash's PREFIX - same string here
// std has no regex in stable; hand-parse the $2y$<cost>$ layout
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 are all bcrypt variants
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 parses a bcrypt hash's PREFIX - same string here
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
Language differences
C: no password-hashing ecosystem (you bring a bcrypt/argon2 implementation) - hash recognition means hand-written prefix parsing (strncmp "$2" layout split).
C++: same as C - hand-write or pull in libbcrypt/argon2; the example splits the cost with a full std::regex match.
C#: .NET has no built-in password hashing (bcrypt/argon2 are NuGet packages) - prefix parsing via Regex; note that fixed is a C# keyword, so the variable must be renamed.
Go: golang.org/x/crypto/bcrypt is the de facto standard - bcrypt.Cost(hash) reads the cost directly; hash recognition via a regex split of the $2y$ prefix.
Java: no built-in password hashing - jBCrypt (BCrypt.hashpw/checkpw) is the convention; prefix parsing with Pattern.
JavaScript: Node has no built-in password hashing - the bcrypt/argon2 npm packages are the convention; prefix parsing with a regex.
PHP: password_get_info only parses the prefix, it does not verify the hash - it returns algo (an integer algorithm id, 2 for bcrypt as measured)/algoName ('bcrypt')/options (cost); an unknown format yields algo=0/algoName=unknown; it closes the loop with password_hash/password_verify/password_needs_rehash - needs_rehash relies on get_info's cost to decide whether to rehash; note the PASSWORD_BCRYPT/PASSWORD_DEFAULT constants themselves are the string '2y' (measured on 8.6), not the same notation as get_info's integer algo.
Python: no password hashing in stdlib - the bcrypt/argon2-cffi packages are the convention; prefix parsing with re; the passlib library can even identify all formats in reverse.
Rust: the bcrypt crate (already on the dependency list) or argon2; stable std has no regex - hand-split the $2y$<cost>$ layout.
TypeScript: same as JavaScript - the bcrypt/argon2 npm packages.