REGEXVAULTv2.0
Security/Password Formats
Verified Safe

PBKDF2 Hash (Django / passlib format) Regex for JavaScript

/^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 JavaScript. 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 JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.

Javascript Implementation

Javascript
// PBKDF2 Hash (Django / passlib format)
// ReDoS-safe | RegexVault — Security > Password Formats

const pbkdf2HashDjangoPasslibFormatRegex = /^pbkdf2_sha(256|512)\$([0-9]+)\$([A-Za-z0-9+\/=]{1,32})\$([A-Za-z0-9+\/=]{43,86})$/;

function validatePbkdf2HashDjangoPasslibFormat(input: string): boolean {
  return pbkdf2HashDjangoPasslibFormatRegex.test(input);
}

// Example
console.log(validatePbkdf2HashDjangoPasslibFormat("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 JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. 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