PBKDF2 Hash (Django / passlib format) Regex for Java
/^pbkdf2_sha(256|512)\$([0-9]+)\$([A-Za-z0-9+/=]{1,32})\$([A-Za-z0-9+/=]{43,86})$/What this pattern does
This page provides a well-structured, multi-part regular expression for matching pbkdf2 hash (django / passlib format), 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
// PBKDF2 Hash (Django / passlib format)
// ReDoS-safe | RegexVault — Security > Password Formats
import java.util.regex.Pattern;
public class Pbkdf2HashDjangoPasslibFormatValidator {
private static final Pattern PATTERN =
Pattern.compile("^pbkdf2_sha(256|512)\\$([0-9]+)\\$([A-Za-z0-9+/=]{1,32})\\$([A-Za-z0-9+/=]{43,86})$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("pbkdf2_sha256$390000$abcdefghijklmnop$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN01234567890=")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
pbkdf2_sha256$390000$abcdefghijklmnop$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN01234567890= | pbkdf2_sha256$390000$short$hash |
| — | pbkdf2_md5$390000$abcdefghijklmnop$hash |
| — | pbkdf2_sha256$ABC$abcdefghijklmnop$hash |
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
The iteration count is the key security parameter. Old hashes with iteration counts below 100000 are vulnerable to GPU cracking. Migrate hashes to higher iteration counts by rehashing on successful login.
Technical Notes
Format: algorithm + $ + iterations + $ + salt (base64) + $ + hash (base64). Django default uses sha256 with 390000 iterations (Django 4.2). NIST SP 800-63b recommends at least 10000 iterations — modern systems should use much higher. SHA-512 is preferred over SHA-256.
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