REGEXVAULTv2.0
Security/Password Formats
Verified Safe

SHA-256 Hash Regex for PHP

/^[a-f0-9]{64}$/i

What this pattern does

This page provides a lightweight, single-purpose regular expression for matching sha-256 hash, ported and verified for PHP. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your PHP project — whether you're validating in a Laravel validator, a WordPress plugin, or a standalone PHP script.

Php Implementation

Php
<?php
// SHA-256 Hash
// ReDoS-safe | RegexVault — Security > Password Formats

define('SHA256_HASH_PATTERN', '/^[a-f0-9]{64}$/');

function validate_sha256_hash(string $input): bool {
    return (bool) preg_match(SHA256_HASH_PATTERN, $input);
}

// Example
var_dump(validate_sha256_hash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855X

When to use this pattern

This pattern is drawn from the Security > Password Formats category and carries a ReDoS-safe certification. That matters for PHP developers because especially relevant in PHP where PCRE backtracking limits can trigger silent failures on malicious input. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

SHA-256 of a password without salt is equivalent to MD5 for dictionary attacks — it is fast to compute and has no iteration cost. Always use a proper KDF (Key Derivation Function) for password storage.

Technical Notes

SHA-256 is part of the SHA-2 family, still cryptographically secure. Used for certificates, code signing, TLS, and file integrity. For password hashing, always use SHA-256 within a proper password hashing function (PBKDF2, Argon2) — raw SHA-256 alone is not suitable for passwords.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern