What PBKDF2 is
PBKDF2 (Password-Based Key Derivation Function 2) turns a password into a key or a stored password hash. It is defined in RFC 8018 (PKCS #5 version 2.1) and approved by NIST in SP 800-132.
Its job is to be slow on purpose. It runs a pseudorandom function, usually HMAC with SHA-256 or SHA-512, many thousands of times. A server pays that cost once per sign-in. An attacker trying billions of guesses against a stolen hash pays it for every guess.
It also takes a salt: random bytes stored next to the hash, different for every password. The salt makes identical passwords produce different hashes, so one precomputed table cannot crack many accounts at once.
How PBKDF2 works
For each block of output, PBKDF2 computes HMAC of the salt plus a block number, then HMACs that result again and again for the given number of iterations, XORing every round into the block. The blocks are joined and cut to the requested length.
U1 = HMAC(password, salt || INT(i))
U2 = HMAC(password, U1)
...
Uc = HMAC(password, Uc-1)
T_i = U1 xor U2 xor ... xor Uc (c = iteration count)
DK = T_1 || T_2 || ... cut to the key lengthpassword: sample password
salt: c2FtcGxlLXNhbHQtMTIzNA== (Base64 of "sample-salt-1234")
PRF: HMAC-SHA256
iterations: 600000
length: 32 bytes
result: d+KGo0IzPTKJyLQW8igawz0Z+TzT1+wIRiNdROZPWYQ=Why not ask for a longer output
Each block of output costs the full iteration count. Asking for more output than the hash size, such as 64 bytes from HMAC-SHA256, doubles the server’s work but not the attacker’s, who can check a guess against the first block alone. Keep the output at or below the hash size: 32 bytes for SHA-256, 64 for SHA-512.
How many iterations to use
The iteration count sets the cost. OWASP’s Password Storage Cheat Sheet gives these minimums for PBKDF2:
| PRF | OWASP minimum iterations |
|---|---|
| HMAC-SHA256 | 600,000 |
| HMAC-SHA512 | 210,000 |
| HMAC-SHA1 | 1,300,000 |
These figures are tuned so each variant costs an attacker about the same. They are a floor; raise them if your servers can afford it. The tool shows how long one derivation took in your browser, which gives a feel for the cost, though servers and attackers’ GPUs run at very different speeds.
Older defaults such as 1,000 or 10,000 iterations are far too low today. Hashes stored with them should be upgraded, which systems usually do the next time the user signs in.
PBKDF2 compared with other password hashes
| Function | Cost it imposes | Notes |
|---|---|---|
| Argon2id | Time and memory | Winner of the Password Hashing Competition, RFC 9106. OWASP’s first choice. |
| scrypt | Time and memory | Memory hardness slows GPU and ASIC attacks. |
| bcrypt | Time, small memory | Widely available; truncates passwords at 72 bytes. |
| PBKDF2 | Time only | FIPS-approved; the choice when compliance requires FIPS 140 validated algorithms. |
Because PBKDF2 uses almost no memory, GPUs and custom hardware run it in parallel very efficiently. That is the main reason Argon2id is preferred for new systems. PBKDF2 with a high iteration count is still an acceptable choice, and the only one of these with FIPS approval.
PBKDF2 in Keycloak
Keycloak hashes user passwords with a provider chosen in the realm’s password policy. It ships pbkdf2, pbkdf2-sha256 and pbkdf2-sha512 providers, and recent versions add Argon2. The policy’s hashing algorithm and hashing iterations settings choose the provider and the count.
Each stored credential keeps its own algorithm, iteration count and salt. When the policy changes, existing passwords stay valid and Keycloak rehashes a password with the new settings the next time its user signs in.
To build a credential in the exact JSON shape Keycloak stores, for example for a user import, use the Keycloak password hash tool. This page computes the raw PBKDF2 value only.
Changing the hashing policy affects sign-in speed for every user. Test higher iteration counts on hardware like production before rolling them out.
What this tool does and does not do
- It runs PBKDF2 with HMAC-SHA256, HMAC-SHA512 or HMAC-SHA1 using Web Crypto, in your browser.
- It reads the salt as text, hex or Base64, and can make a random 16-byte salt.
- It outputs the derived key in hex, Base64 or Base64url, at any length from 1 to 1,024 bytes.
- It derives once when the page loads and then only when you press Derive, since high iteration counts take a moment.
- It does not produce a full password-hash string such as a PHC string or a Keycloak credential, and it does not do Argon2, bcrypt or scrypt.
How to derive a key
- Enter the password. It is read as UTF-8.
- Enter the salt in the format you have, or press New random salt.
- Choose the hash. The iteration count fills in with OWASP’s minimum for it; change it if you need to match an existing system.
- Set the key length, usually 32 bytes for SHA-256 or 64 for SHA-512.
- Press Derive, then copy the result in the encoding you need.
Matching another system’s hash
When the result here differs from a stored hash, one of five inputs differs. Check each:
- The password bytes. Some systems normalize Unicode or trim whitespace first.
- The salt bytes. A salt stored as Base64 must be decoded, not used as text.
- The hash function, SHA-1, SHA-256 or SHA-512.
- The iteration count.
- The output length and encoding.
node -e "console.log(require('crypto').pbkdf2Sync('sample password', Buffer.from('c2FtcGxlLXNhbHQtMTIzNA==','base64'), 600000, 32, 'sha256').toString('base64'))"Security notes
- Use a unique random salt of at least 16 bytes for every password. Never reuse a salt or derive it from the username.
- The salt is not secret. It is stored with the hash.
- A pepper, a secret added to every password and kept outside the database, can add protection if the database alone leaks. Keep it in a secrets manager, not next to the hashes.
- Never store passwords with a single fast hash such as SHA-256, with or without salt.
- Use real passwords only on a device you trust; the tool sends nothing, but test values are safer.
Best practices
- For new systems, prefer Argon2id; use PBKDF2 where FIPS compliance or compatibility requires it.
- Start from OWASP’s minimum iteration counts and review them every year or two.
- Store the algorithm, iterations and salt with each hash so you can raise the cost later.
- Rehash on sign-in when a stored hash uses outdated settings.
- Rate-limit sign-in and enable brute force protection, so slow hashing cannot be turned into a denial of service.
Moving password hashes between systems
PBKDF2 is common enough that hashes can often move between systems without users resetting their passwords. That matters when migrating users into an identity provider.
Each system stores the same few parts, in its own layout. A typical modular format puts them in one string:
<algorithm>$<iterations>$<salt>$<hash>
pbkdf2_sha256$600000$<salt>$<Base64 hash>To import such hashes into Keycloak, map each part into Keycloak’s credential fields: the algorithm onto a matching provider, the iteration count, the salt and the hash, each in the encoding Keycloak expects. Then test one account by signing in before importing the rest. If the source uses an algorithm Keycloak does not ship, users must reset their passwords or a custom provider must check the old format.