REGEXVAULTv2.0
Security/Password Formats
Verified Safe

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

/^[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 Java. 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 Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

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

import java.util.regex.Pattern;

public class Sha3KeccakHash256And512Validator {
    private static final Pattern PATTERN =
        Pattern.compile("^[a-f0-9]{64}$|^[a-f0-9]{128}$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("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 Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. 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