SHA-256 Hash Regex for Java
/^[a-f0-9]{64}$/iWhat this pattern does
This page provides a lightweight, single-purpose regular expression for matching sha-256 hash, 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
// SHA-256 Hash
// ReDoS-safe | RegexVault — Security > Password Formats
import java.util.regex.Pattern;
public class Sha256HashValidator {
private static final Pattern PATTERN =
Pattern.compile("^[a-f0-9]{64}$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 | e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85 |
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 | e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855X |
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
SHA-256 of a password without salt is equivalent to MD5 for dictionary attacks — it is fast to compute and has no iteration cost. Always use a proper KDF (Key Derivation Function) for password storage.
Technical Notes
SHA-256 is part of the SHA-2 family, still cryptographically secure. Used for certificates, code signing, TLS, and file integrity. For password hashing, always use SHA-256 within a proper password hashing function (PBKDF2, Argon2) — raw SHA-256 alone is not suitable for passwords.
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