SHA-3 / Keccak Hash (256 and 512) Regex for JavaScript
/^[a-f0-9]{64}$|^[a-f0-9]{128}$/iWhat 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 JavaScript. 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 JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// SHA-3 / Keccak Hash (256 and 512)
// ReDoS-safe | RegexVault — Security > Password Formats
const sha3KeccakHash256And512Regex = /^[a-f0-9]{64}$|^[a-f0-9]{128}$/i;
function validateSha3KeccakHash256And512(input: string): boolean {
return sha3KeccakHash256And512Regex.test(input);
}
// Example
console.log(validateSha3KeccakHash256And512("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a | short |
0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e | not_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 JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. 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