SHA-512 Hash Regex for PHP
/^[a-f0-9]{128}$/iWhat this pattern does
This page provides a lightweight, single-purpose regular expression for matching sha-512 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
// SHA-512 Hash
// ReDoS-safe | RegexVault — Security > Password Formats
define('SHA512_HASH_PATTERN', '/^[a-f0-9]{128}$/');
function validate_sha512_hash(string $input): bool {
return (bool) preg_match(SHA512_HASH_PATTERN, $input);
}
// Example
var_dump(validate_sha512_hash("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")); // bool(true)Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e | cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9c |
| — | cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3eXX |
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-512 is computationally slightly faster on 64-bit systems than SHA-256 due to internal 64-bit operations. For general hashing, SHA-256 is more widely supported.
Technical Notes
SHA-512 produces 512-bit (128 hex char) digests. Part of the SHA-2 family. Commonly used for HMAC signatures, certificate fingerprints, and file integrity. For passwords, use within PBKDF2-SHA512 or Argon2.
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