PBKDF2 Explained: How It Works, Iteration Counts, Salt and Key Length
Last updated: · 9 min read
PBKDF2 (Password-Based Key Derivation Function 2) turns a password into a cryptographic key and is slow on purpose. It mixes the password and a salt by running HMAC thousands or hundreds of thousands of times in a chain, so that an attacker guessing passwords has to pay the same cost for every guess. It is defined in RFC 8018 (PKCS #5 v2.1) and is used in disk and file encryption, in the WPA2 Wi-Fi key and in password managers. This guide covers the formula, how to choose the salt and the iteration count, the trap of asking for a long key, and where PBKDF2 stands next to bcrypt and Argon2. The values in the example are real output from the PBKDF2 Key Derivation Tool; the calculation runs in your browser through the Web Crypto API.
Formula
DK = PBKDF2(PRF, P, S, c, dkLen) P = password, S = salt, c = iteration count, dkLen = requested key length (bytes) PRF = HMAC-SHA-256, HMAC-SHA-1, HMAC-SHA-384 or HMAC-SHA-512 hLen = PRF output: 20 bytes (SHA-1), 32 (SHA-256), 48 (SHA-384), 64 (SHA-512) l = ⌈dkLen / hLen⌉ (number of blocks needed) U_1 = PRF(P, S ‖ INT(i)) (i: four-byte block index, starting at 1) U_j = PRF(P, U_(j−1)) (j = 2 … c) T_i = U_1 ⊕ U_2 ⊕ … ⊕ U_c DK = T_1 ‖ T_2 ‖ … ‖ T_l → first dkLen bytes Total work = c × l HMAC chains
For each output block, HMAC is run c times in a row with the password as its key: the first round hashes the salt and the block index, every later round hashes the output of the round before, and all intermediate results are combined with XOR. If the requested key is longer than the hash output, the whole process is repeated for block 2, 3 and so on, the blocks are concatenated and the excess is cut off. The salt makes the same password yield different keys in different records and renders precomputed tables useless. The iteration count sets the running time directly: doubling it doubles the time for you and for the attacker alike. The tool converts the password and the salt to UTF-8 bytes, derives the key with the browser's SubtleCrypto.deriveBits and shows the same bytes as hex and as Base64.
What the inputs mean
- Password
- The password the key is derived from. The characters you type are used as UTF-8 text; leading and trailing spaces are kept and case matters. It cannot be empty.
- Salt
- A value that should be random and different for every record; it does not need to be secret. The tool reads the salt as UTF-8 text too: a salt written in hex or Base64 is not decoded, its characters are used as they are. It cannot be empty.
- Iterations
- The length of the HMAC chain: a whole number from 1 to 10,000,000, 600,000 by default. The larger the value, the longer the calculation takes.
- Key length (bytes)
- The number of bytes to derive, from 1 to 512; the default is 32 bytes (256 bits). Asking for more than the hash output raises the block count and the workload in proportion; iterations multiplied by the number of blocks may not exceed 20,000,000.
- Hash function
- HMAC-SHA-256 (default), HMAC-SHA-1, HMAC-SHA-384 or HMAC-SHA-512. Pick whichever the system you are comparing against uses; changing the hash changes the entire output.
Worked example: "password" and "salt" at 600,000 iterations, then an RFC 6070 test vector
- Input: Password = password, Salt = salt, Iterations = 600000, Key length = 32, Hash function = HMAC-SHA-256. These are the tool's defaults.
- A 32-byte key fits into the 32-byte output of SHA-256, so one block is enough (l = 1). The tool starts with U_1 = HMAC(password, salt ‖ 00 00 00 01), runs HMAC 600,000 times in total and XORs the results.
- The output begins with the heading PBKDF2-HMAC-SHA-256, followed by Hex: 669cfe52482116fda1aa2cbe409b2f56c8e4563752b7a28f6eaab614ee005178 and Base64: Zpz+UkghFv2hqiy+QJsvVsjkVjdSt6KPbqq2FO4AUXg=
- The next lines repeat the settings: Iterations: 600000 and Key length: 32 bytes (256 bit). At the bottom comes a comparison with the widely used OWASP guidance figure: OWASP guidance: at least 600,000 iterations for PBKDF2-HMAC-SHA-256. Your value meets this minimum.
- Capitalise just the first letter of the salt (Salt) and the hex value becomes 2316edad213524201b93ab9e66b682bd1a50aaec753378d9bc106b1220b1290b. One different character in the salt changes the whole key.
- Set the key length to 16 and the result is 669cfe52482116fda1aa2cbe409b2f56, the first half of the 32-byte key. PBKDF2 produces a shorter key by truncating the longer output.
- Checking against the official value: the first test case in RFC 6070 uses password password, salt salt, 1 iteration, a length of 20 bytes and HMAC-SHA-1. The tool returns 0c60c80f961f0e71f3a9b524af6012062fe037a6, the value printed in the RFC. With 4096 iterations it returns 4b007901b765489abead49d926f721d065a429c1, which matches the RFC's third case.
- For both of these test cases the tool also shows this notice: OWASP guidance: at least 1,300,000 iterations for PBKDF2-HMAC-SHA-1. Your value is below this minimum.
- Limit check: with 10000000 iterations and a key length of 512, SHA-256 needs 16 blocks, and the tool stops before calculating anything: Workload too large: iterations × number of hash blocks can be at most 20,000,000. Lower the iteration count or the key length.
Result: PBKDF2-HMAC-SHA-256("password", "salt", 600000, 32 bytes) = 669cfe52482116fda1aa2cbe409b2f56c8e4563752b7a28f6eaab614ee005178
🗝️ PBKDF2 Key Derivation Tool
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
There is no single correct iteration count; the aim is the longest running time you can accept on your target hardware. RFC 8018 asks for the count to be as large as users will tolerate and mentions a minimum of 1,000, a floor that is historical and far too low for current hardware. The figures the tool displays, 600,000 (SHA-256), 210,000 (SHA-512) and 1,300,000 (SHA-1), are OWASP's widely cited guidance values. They are not numbers mandated by a standards body, and they are raised as hardware gets faster. PBKDF2's weakness is that it only costs CPU time: it needs almost no memory, so graphics cards and custom chips can run a great many guesses in parallel. Memory-hard functions such as Argon2id (RFC 9106) resist this better for password storage, and bcrypt is a long-established option built for the same job. PBKDF2 remains a sound choice where FIPS compliance is required or where only standard libraries such as Web Crypto are available. No iteration count rescues a weak password. The tool does not send the password anywhere; even so, make it a habit not to type real passwords into web pages.
Common mistakes
- Using the same salt for every user, or no salt at all. A salt must be random and unique per record (RFC 8018 recommends at least 8 bytes; 16 bytes is common in practice) and is stored in the clear next to the derived value.
- Asking for a key longer than the hash output. With SHA-256 and 64 bytes the defender computes two blocks, while an attacker testing a password only needs the first 32 bytes: your workload doubles and security does not improve. If you need a longer key, choose SHA-512 or expand the output with HKDF.
- Sticking with the 1,000 or 10,000 iterations found in old tutorials. Those values were chosen for hardware of years ago; store the iteration count with each record so that you can raise it later.
- Assuming the salt is decoded from hex or Base64. This tool reads the salt as plain text; if another library turns the same salt into raw bytes first, the results will not match.
- Ignoring the character encoding of the password. The tool uses UTF-8; a password containing letters such as é or ü gives a completely different key when encoded as UTF-16 or Windows-1252. Unicode normalisation (composed versus decomposed forms) changes the result as well.
- Comparing derived values with ==. Use a constant-time comparison when verifying a password: crypto.timingSafeEqual in Node.js, hmac.compare_digest in Python, hash_equals in PHP.
- Using PBKDF2 to stretch a key that already has high entropy. If the input is already a random key there is nothing to slow down; HKDF is designed for that job.
Frequently asked questions
How many iterations should I use for PBKDF2?
Measure on your target server or device and choose the largest value that fits the delay your users will accept, often somewhere between 100 milliseconds and 1 second per login. As a starting point you can use the common guidance figures the tool shows: 600,000 for HMAC-SHA-256 and 210,000 for HMAC-SHA-512. The minimum of 1,000 in RFC 8018 is historical; the same document says a count of 10,000,000 may be appropriate for especially critical keys.
PBKDF2, bcrypt or Argon2: which should I use?
For storing user passwords in a new system, the memory-hard Argon2id is generally the first choice, and bcrypt is a robust option with a long track record. Because PBKDF2 only consumes CPU time it is less resistant to GPU-based attacks, but it is available on every platform and can be built from FIPS-approved components. Whichever you pick, tuning the parameters to your hardware matters as much as the choice of algorithm.
Does the salt have to be secret?
No. The salt's job is uniqueness, not secrecy: it stops the same password giving the same result in two records and prevents the use of precomputed tables. It is stored in the clear with the derived value. An additional value that is kept secret is called a pepper and is managed separately.
Can a password be recovered from PBKDF2 output?
The function cannot be reversed, but an attacker can try candidate passwords one by one with the same salt and iteration count. The iteration count makes those attempts expensive, not impossible. Short passwords and dictionary words can be found even at high iteration counts.
Why do I get a different result in another tool with the same inputs?
The usual causes: a different hash function (some libraries default to SHA-1), the salt read as text in one place and as hex-decoded bytes in the other, a different iteration count or key length, or an invisible trailing newline in the password. Testing the tool with the RFC 6070 vector first and then switching to your own values makes the difference easier to find.
Is PBKDF2-HMAC-SHA-1 insecure?
The known collision attacks on SHA-1 do not directly break PBKDF2, because SHA-1 is used inside HMAC here. New designs nevertheless use SHA-256 or SHA-512; the SHA-1 option exists for compatibility with older systems such as WPA2, and it needs more iterations for the same resistance.
Is my password sent to a server?
No. The key is derived on your device by the Web Crypto API in your browser; the password and the salt are not sent anywhere. Web Crypto is only available over a secure connection (https). At high iteration counts the calculation can take a few seconds.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.
Sources
- RFC 8018 — PKCS #5: Password-Based Cryptography Specification Version 2.1 (PBKDF2: Section 5.2; salt and iteration count: Section 4)
- RFC 6070 — PKCS #5: Password-Based Key Derivation Function 2 (PBKDF2) Test Vectors
- NIST SP 800-132 — Recommendation for Password-Based Key Derivation, Part 1: Storage Applications
- W3C — Web Cryptography API (PBKDF2 and deriveBits)
- RFC 9106 — Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work Applications