password_verify

password_verify(string $password, string $hash): bool

Checks whether a password matches the hash string produced by password_hash, returning true/false.

C C23

C23
#include <stdio.h>
#include <string.h>
#include "sha256.h" // vendored SHA-256 (Brad Conte, public domain)

// HMAC-SHA256 (RFC 2104) on top of the vendored SHA-256; key <= 64 bytes assumed
static void hmac_sha256(const BYTE *key, size_t keylen, const BYTE *msg, size_t msglen, BYTE out[32]) {
    BYTE k[64] = {0}, ipad[64], opad[64], inner[32];
    SHA256_CTX ctx;
    memcpy(k, key, keylen);
    for (int i = 0; i < 64; i++) {
        ipad[i] = k[i] ^ 0x36;
        opad[i] = k[i] ^ 0x5c;
    }
    sha256_init(&ctx);
    sha256_update(&ctx, ipad, 64);
    sha256_update(&ctx, msg, msglen);
    sha256_final(&ctx, inner);
    sha256_init(&ctx);
    sha256_update(&ctx, opad, 64);
    sha256_update(&ctx, inner, 32);
    sha256_final(&ctx, out);
}

// PBKDF2-HMAC-SHA256 (RFC 2898), single-block dkLen = 32 case
static void pbkdf2(const char *pass, const char *salt, int iters, BYTE out[32]) {
    BYTE u[32], t[32], msg[64];
    size_t passlen = strlen(pass), saltlen = strlen(salt);
    memcpy(msg, salt, saltlen);
    msg[saltlen] = 0;
    msg[saltlen + 1] = 0;
    msg[saltlen + 2] = 0;
    msg[saltlen + 3] = 1; // block index INT(1), big-endian
    hmac_sha256((const BYTE *)pass, passlen, msg, saltlen + 4, u);
    memcpy(t, u, 32);
    for (int i = 1; i < iters; i++) {
        hmac_sha256((const BYTE *)pass, passlen, u, 32, u);
        for (int j = 0; j < 32; j++) t[j] ^= u[j];
    }
    memcpy(out, t, 32);
}

// verify: re-derive the digest from the password and compare with the stored string
static int verify(const char *password, const char *stored) {
    BYTE dk[32];
    char hex[65] = {0};
    pbkdf2(password, "codeabc-salt", 1000, dk);
    for (int i = 0; i < 32; i++) sprintf(hex + i * 2, "%02x", dk[i]);
    return strcmp(hex, stored) == 0; // demo compares strings; real code compares constant-time
}

int main(void) {
    const char *stored = "0a6eab55c375788ed4268a5e7d0ce7ea7ff56b1d3f3f10427763fa554d6b2bcd";
    printf("%s\n", verify("secret", stored) ? "true" : "false");
    printf("%s\n", verify("Secret", stored) ? "true" : "false");
    // a plain md5 hex string is not a recognized format: verify returns false
    printf("%s\n", verify("secret", "5f4dcc3b5aa765d61d8327deb882cf99") ? "true" : "false");
    return 0;
}
Output
true
false
false

C++ C++26

C++26
#include <cstdio>
#include <cstring>
#include "sha256.h" // vendored SHA-256 (Brad Conte, public domain)

// HMAC-SHA256 (RFC 2104); key <= 64 bytes assumed
static void hmac_sha256(const BYTE *key, size_t keylen, const BYTE *msg, size_t msglen, BYTE out[32]) {
    BYTE k[64] = {0}, ipad[64], opad[64], inner[32];
    SHA256_CTX ctx;
    memcpy(k, key, keylen);
    for (int i = 0; i < 64; i++) {
        ipad[i] = k[i] ^ 0x36;
        opad[i] = k[i] ^ 0x5c;
    }
    sha256_init(&ctx);
    sha256_update(&ctx, ipad, 64);
    sha256_update(&ctx, msg, msglen);
    sha256_final(&ctx, inner);
    sha256_init(&ctx);
    sha256_update(&ctx, opad, 64);
    sha256_update(&ctx, inner, 32);
    sha256_final(&ctx, out);
}

// PBKDF2-HMAC-SHA256 (RFC 2898), single-block dkLen = 32 case
static void pbkdf2(const char *pass, const char *salt, int iters, BYTE out[32]) {
    BYTE u[32], t[32], msg[64];
    size_t passlen = strlen(pass), saltlen = strlen(salt);
    memcpy(msg, salt, saltlen);
    msg[saltlen] = 0;
    msg[saltlen + 1] = 0;
    msg[saltlen + 2] = 0;
    msg[saltlen + 3] = 1; // block index INT(1), big-endian
    hmac_sha256((const BYTE *)pass, passlen, msg, saltlen + 4, u);
    memcpy(t, u, 32);
    for (int i = 1; i < iters; i++) {
        hmac_sha256((const BYTE *)pass, passlen, u, 32, u);
        for (int j = 0; j < 32; j++) t[j] ^= u[j];
    }
    memcpy(out, t, 32);
}

// verify: re-derive the digest from the password and compare with the stored string
static bool verify(const char *password, const char *stored) {
    BYTE dk[32];
    char hex[65] = {0};
    pbkdf2(password, "codeabc-salt", 1000, dk);
    for (int i = 0; i < 32; i++) sprintf(hex + i * 2, "%02x", dk[i]);
    return strcmp(hex, stored) == 0;
}

int main() {
    const char *stored = "0a6eab55c375788ed4268a5e7d0ce7ea7ff56b1d3f3f10427763fa554d6b2bcd";
    printf("%s\n", verify("secret", stored) ? "true" : "false");
    printf("%s\n", verify("Secret", stored) ? "true" : "false");
    // a plain md5 hex string is not a recognized format: verify returns false
    printf("%s\n", verify("secret", "5f4dcc3b5aa765d61d8327deb882cf99") ? "true" : "false");
    return 0;
}
Output
true
false
false

C# v11.0.100

v11.0.100
using System.Security.Cryptography;
using System.Text;

// .NET has no built-in bcrypt; PBKDF2 (Rfc2898DeriveBytes.Pbkdf2) is the standard choice.
// Verify = re-derive with the stored salt/params, then constant-time compare.
byte[] stored = Convert.FromHexString("0a6eab55c375788ed4268a5e7d0ce7ea7ff56b1d3f3f10427763fa554d6b2bcd");
byte[] salt = Encoding.UTF8.GetBytes("codeabc-salt");

bool Verify(string password)
{
    byte[] candidate = Rfc2898DeriveBytes.Pbkdf2(password, salt, 1000, HashAlgorithmName.SHA256, 32);
    return CryptographicOperations.FixedTimeEquals(candidate, stored); // constant-time compare
}

Console.WriteLine(Verify("secret"));
Console.WriteLine(Verify("Secret"));
// a plain md5 hex string is not a recognized format: verify yields false
Console.WriteLine(Verify("5f4dcc3b5aa765d61d8327deb882cf99"));
Output
True
False
False

Go v1.27.4

v1.27.4
package main

import (
    "fmt"

    "golang.org/x/crypto/bcrypt" // ecosystem standard; verifies PHP bcrypt hashes directly
)

func main() {
    hash := "$2y$12$/sXs8iV/A.GUMFTirj8h3e3P7mcfjPAEZHrPnCrNgff447C9eQthm"
    err := bcrypt.CompareHashAndPassword([]byte(hash), []byte("secret"))
    fmt.Println(err == nil) // nil error means the password matches
    err2 := bcrypt.CompareHashAndPassword([]byte(hash), []byte("Secret"))
    fmt.Println(err2 == nil)
    // a plain md5 hex string is not a bcrypt format: compare fails, so false
    err3 := bcrypt.CompareHashAndPassword([]byte("5f4dcc3b5aa765d61d8327deb882cf99"), []byte("secret"))
    fmt.Println(err3 == nil)
}
Output
true
false
false

Java v27.0.1

v27.0.1
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HexFormat;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class Main {
    // Java has no built-in bcrypt; PBKDF2WithHmacSHA256 is the standard choice.
    // Verify = re-derive with the stored salt/params, then constant-time compare.
    static boolean verify(String password, byte[] stored) throws Exception {
        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(),
                "codeabc-salt".getBytes(StandardCharsets.UTF_8), 1000, 256);
        byte[] candidate = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
                .generateSecret(spec).getEncoded();
        return MessageDigest.isEqual(candidate, stored); // constant-time compare
    }

    public static void main(String[] args) throws Exception {
        byte[] stored = HexFormat.of().parseHex("0a6eab55c375788ed4268a5e7d0ce7ea7ff56b1d3f3f10427763fa554d6b2bcd");
        System.out.println(verify("secret", stored));
        System.out.println(verify("Secret", stored));
        // a plain md5 hex string is not a recognized format: verify yields false
        System.out.println(verify("5f4dcc3b5aa765d61d8327deb882cf99", stored));
    }
}
Output
true
false
false

JavaScript v26.13.1

v26.13.1
const { pbkdf2Sync, timingSafeEqual } = require("node:crypto");

// Node has no built-in bcrypt; pbkdf2 (or scrypt) is the standard choice.
// Verify = re-derive with the stored salt/params, then constant-time compare.
const stored = Buffer.from("0a6eab55c375788ed4268a5e7d0ce7ea7ff56b1d3f3f10427763fa554d6b2bcd", "hex");

function verify(password) {
  const candidate = pbkdf2Sync(password, "codeabc-salt", 1000, 32, "sha256");
  return timingSafeEqual(candidate, stored); // constant-time, prevents timing attacks
}

console.log(verify("secret"));
console.log(verify("Secret"));
// a plain md5 hex string is not a recognized format: verify yields false
console.log(verify("5f4dcc3b5aa765d61d8327deb882cf99"));
Output
true
false
false

PHP v8.6.0

v8.6.0
// password_hash() hashes with bcrypt and a random salt, so every stored hash is
// different; verify reads the algorithm/salt from the string itself and re-checks
$hash = '$2y$12$/sXs8iV/A.GUMFTirj8h3e3P7mcfjPAEZHrPnCrNgff447C9eQthm';
var_dump(password_verify('secret', $hash)); // correct password
var_dump(password_verify('Secret', $hash)); // wrong password (case differs)
var_dump(password_verify('secret', '5f4dcc3b5aa765d61d8327deb882cf99')); // plain md5 hex: false
Output
bool(true)
bool(false)
bool(false)

Python v3.15.2

v3.15.2
import hashlib
import hmac

# Python has no built-in bcrypt; pbkdf2_hmac is the standard-library choice.
# Verify = re-derive with the stored salt/params, then constant-time compare.
stored = bytes.fromhex("0a6eab55c375788ed4268a5e7d0ce7ea7ff56b1d3f3f10427763fa554d6b2bcd")


def verify(password: str) -> bool:
    candidate = hashlib.pbkdf2_hmac("sha256", password.encode(), b"codeabc-salt", 1000)
    return hmac.compare_digest(candidate, stored)  # constant-time compare


print(verify("secret"))
print(verify("Secret"))
# a plain md5 hex string is not a recognized format: verify yields false
print(verify("5f4dcc3b5aa765d61d8327deb882cf99"))
Output
True
False
False

Rust v1.99.1

v1.99.1
use bcrypt; // bcrypt crate: verifies PHP $2y$ hashes directly

fn main() {
    let hash = "$2y$12$/sXs8iV/A.GUMFTirj8h3e3P7mcfjPAEZHrPnCrNgff447C9eQthm";
    println!("{}", bcrypt::verify("secret", hash).unwrap_or(false));
    println!("{}", bcrypt::verify("Secret", hash).unwrap_or(false));
    // a plain md5 hex string is not a bcrypt format: verify errors, so false
    println!("{}", bcrypt::verify("secret", "5f4dcc3b5aa765d61d8327deb882cf99").unwrap_or(false));
}
Output
true
false
false

TypeScript v26.13.1

v26.13.1
import { pbkdf2Sync, timingSafeEqual } from "node:crypto";

// Node has no built-in bcrypt; pbkdf2 (or scrypt) is the standard choice.
const stored: Buffer = Buffer.from(
  "0a6eab55c375788ed4268a5e7d0ce7ea7ff56b1d3f3f10427763fa554d6b2bcd",
  "hex"
);

function verify(password: string): boolean {
  const candidate = pbkdf2Sync(password, "codeabc-salt", 1000, 32, "sha256");
  return timingSafeEqual(candidate, stored); // constant-time compare
}

console.log(verify("secret"));
console.log(verify("Secret"));
// a plain md5 hex string is not a recognized format: verify yields false
console.log(verify("5f4dcc3b5aa765d61d8327deb882cf99"));
Output
true
false
false

Language differences

C: no built-in password hashing — hand-written PBKDF2 on the vendored SHA-256, re-derive then compare; the demo uses strcmp, real code should compare constant-time.

C++: same as C, hand-written PBKDF2 on the vendored SHA-256.

C#: PBKDF2 re-derive + constant-time FixedTimeEquals.

Go: bcrypt route — CompareHashAndPassword verifies PHP $2y$ hashes directly; a plain md5 string errors out as too short, equivalent to false.

Java: PBKDF2 re-derive + constant-time MessageDigest.isEqual.

JavaScript: PBKDF2 re-derive + constant-time timingSafeEqual.

PHP: password_verify() pairs strictly with password_hash() — the hash string carries algorithm/cost/salt/checksum, and verify reads them from the string to re-derive; no separate salt column needed; in real use every user's salt differs, so the same password hashes differently per user row (defeating rainbow tables); a non-crypt-format string (e.g. legacy md5 hex) always yields false — no "compatibility" with plain hashes; migrating old hashes requires comparing them separately before rehashing on next login; failure is just false, with no distinction between "wrong password" and "bad format".

Python: PBKDF2 re-derive + constant-time hmac.compare_digest.

Rust: the bcrypt crate's verify checks PHP $2y$ hashes directly; invalid formats return Err, normalized to false via unwrap_or(false).

TypeScript: same as JS — Node PBKDF2 with timingSafeEqual.