REGEXVAULTv2.0
Security/Password Formats
Verified Safe

SHA-3 / Keccak Hash (256 and 512) Regex for PHP

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

What this pattern does

This page provides a well-structured, multi-part regular expression for matching sha-3 / keccak hash (256 and 512), 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-3 / Keccak Hash (256 and 512)
// ReDoS-safe | RegexVault — Security > Password Formats

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

function validate_sha3_keccak_hash_256_and_512(string $input): bool {
    return (bool) preg_match(SHA3_KECCAK_HASH_256_AND_512_PATTERN, $input);
}

// Example
var_dump(validate_sha3_keccak_hash_256_and_512("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")); // bool(true)

Test Cases

Matches (Valid)
Rejects (Invalid)
a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434ashort
0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680enot_hex_chars!!
a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434

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

Ethereum uses Keccak-256 (a variant of SHA-3 that predates the final FIPS 202 standardization). Keccak-256 ≠ SHA3-256. Ethereum addresses use the Keccak-256 variant, which produces different output than standard SHA3-256.

Technical Notes

SHA-3 (Standardized 2015, FIPS 202) uses a completely different internal construction (Keccak sponge) from SHA-2. Resistant to length-extension attacks that affect SHA-2. Note: SHA3-256 and SHA-256 produce the same output length (64 hex) — distinguish by context/system documentation.

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