REGEXVAULTv2.0
Security/Password Formats
Verified Safe

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

/^[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 Python. 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 Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.

Python Implementation

Python
# SHA-3 / Keccak Hash (256 and 512)
# ReDoS-safe | RegexVault — Security > Password Formats

import re

sha3_keccak_hash_256_and_512_pattern = re.compile(r'^[a-f0-9]{64}$|^[a-f0-9]{128}$')

def validate_sha3_keccak_hash_256_and_512(value: str) -> bool:
    return bool(sha3_keccak_hash_256_and_512_pattern.fullmatch(value))

# Example
print(validate_sha3_keccak_hash_256_and_512("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"))  # 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 Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. 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