MD5 Hash (Deprecated — Detection Only) Regex for Java
/^[a-f0-9]{32}$/iWhat this pattern does
This page provides a lightweight, single-purpose regular expression for matching md5 hash (deprecated — detection only), 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
// MD5 Hash (Deprecated — Detection Only)
// ReDoS-safe | RegexVault — Security > Password Formats
import java.util.regex.Pattern;
public class Md5HashDeprecatedDetectionOnlyValidator {
private static final Pattern PATTERN =
Pattern.compile("^[a-f0-9]{32}$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("d41d8cd98f00b204e9800998ecf8427e")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
d41d8cd98f00b204e9800998ecf8427e | d41d8cd98f00b204e9800998ecf8427 |
098f6bcd4621d373cade4e832627b4f6 | d41d8cd98f00b204e9800998ecf8427eX |
| — | ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ |
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
MD5 hashes of passwords can be cracked in milliseconds with GPU rainbow tables for passwords under 10 characters. A database of MD5-hashed passwords is effectively a plaintext database for short passwords.
Technical Notes
MD5 is cryptographically broken — collision attacks are trivial, preimage attacks are feasible. Never use MD5 for password hashing or security purposes. Use only for checksums where collision resistance is not required (e.g., non-security file deduplication). Include this pattern only for detection/migration purposes.
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