Key Derivation: PBKDF2 and HKDF, Explained

After reading this you will know when to reach for PBKDF2 versus HKDF, how each function turns input into key bytes, and how to pick a salt, iteration count, info string and output length without weakening the result.

What key derivation does

A key derivation function (KDF) takes some input and produces key bytes of a length you choose. The two functions on this page solve two different problems, and mixing them up is the most common mistake in this area.

PBKDF2 (Password-Based Key Derivation Function 2, defined in RFC 8018) turns a low-entropy password like correct horse into a fixed-length key. It is deliberately slow so that an attacker who steals your stored value cannot test billions of password guesses per second.

HKDF (HMAC-based KDF, defined in RFC 5869) does the opposite. It assumes the input is already random and high-entropy, such as a 32-byte shared secret from an Elliptic Curve Diffie-Hellman exchange. It is fast on purpose, and it can produce several independent subkeys from one secret.

Here is the hook: feed the password password with salt salt into PBKDF2-HMAC-SHA256 at 600000 iterations, ask for 32 bytes, and you always get the same 64 hex characters. Change the salt by one byte and every output byte changes. That determinism is the point. You never store the key, you re-derive it.

When to use each one

Choose by asking one question: is the input a human-chosen secret, or is it already random?

Use PBKDF2 when
The input is a password, passphrase or PIN. You are verifying a login, or deriving an encryption key from something a person typed. The input has maybe 30 to 60 bits of entropy, so the slowness matters.
Use HKDF when
The input is already a strong secret: an ECDH result, a random 256-bit master key, or output from a hardware RNG. You want to split it into an encryption key plus a MAC key, or bind it to a context. Speed is fine because guessing the input is already infeasible.

Never run HKDF on a password. HKDF does no stretching, so a weak password stays weak: an attacker tests guesses at the full speed of one HMAC per attempt, which is millions per second on a laptop. Never run PBKDF2 as your only step on a real random secret either, since the iteration count buys you nothing there and just wastes time. Match the tool to the input.

The math, with intuition

PBKDF2 builds its output from a pseudorandom function, almost always HMAC with a chosen hash. For each output block it computes a chain of HMAC calls and folds them together with XOR. For a single block the definition is:

T_1 = U_1 \oplus U_2 \oplus \cdots \oplus U_c

Here c is the iteration count. The first term is U_1 = \text{HMAC}(P, S \mathbin\Vert \text{INT}(1)), where P is the password, S is the salt, and \text{INT}(1) is the block index as four bytes. Each later term feeds the previous one back in: U_i = \text{HMAC}(P, U_{i-1}). The cost is exactly c HMAC evaluations per output block, and that is the knob that slows attackers.

HKDF is two stages, extract then expand:

\text{PRK} = \text{HMAC}(\text{salt}, \text{IKM})

The extract step takes the input key material \text{IKM} and a salt, and produces a fixed-size pseudorandom key \text{PRK} (32 bytes for SHA-256). This concentrates whatever entropy is in the input into a uniform key. The expand step then stretches \text{PRK} to the length you want:

T_i = \text{HMAC}(\text{PRK}, T_{i-1} \mathbin\Vert \text{info} \mathbin\Vert i)

The output is T_1 \mathbin\Vert T_2 \mathbin\Vert \cdots truncated to your requested length, with T_0 empty. The info string is a context label. Two calls with the same PRK but different info give unrelated keys, which is how you get an encryption key and a MAC key from one secret.

Reproducing the PBKDF2 demo

The demo uses password password, salt salt, hash SHA-256, 600000 iterations, and 32 output bytes. These are the tool defaults, so one click reproduces them.

  1. Encode the password and salt as UTF-8 bytes: 70 61 73 73 77 6f 72 64 and 73 61 6c 74.
  2. Compute U_1 = \text{HMAC-SHA256}(P, \, 73\,61\,6c\,74\,00\,00\,00\,01). The salt is followed by the block index 1 as four bytes.
  3. Set U_2 = \text{HMAC-SHA256}(P, U_1), then U_3 from U_2, and so on for 600000 rounds.
  4. XOR all 600000 U_i values together to get the 32-byte output.

The result is 669cfe52482eb2f74650c42c65d5f0a2e... and continues for 64 hex characters. One block of 32 bytes is enough for a SHA-256 key, so only block index 1 is used. If you asked for 48 bytes, the tool would also compute block 2 and append its first 16 bytes.

PBKDF2 cost is linear in the iteration count. At 600000 iterations and a machine that does 3 million HMAC-SHA256 per second, one derivation takes about 0.2 seconds. An attacker with a GPU doing 3 billion per second tests about 5000 password guesses per second, so a 40-bit password space of roughly 1.1 trillion guesses takes about 7 years.

Reading the derived bytes

The output is raw bytes shown two ways. Hex uses two characters per byte, so 32 bytes is 64 hex characters. Base64 packs 3 bytes into 4 characters, so 32 bytes becomes 44 characters including one = pad. Both encode the exact same bytes. Pick whichever your target library expects.

Output length for common uses (SHA-256 KDFs)
UseBytesHex charsBase64 chars
AES-128 key163224
AES-256 key326444
Enc + MAC key pair6412888

The bytes should look uniformly random. Every value from 00 to ff is equally likely, so in 32 bytes you expect no visible pattern and roughly 8 to 10 distinct high nibbles. If you see runs of zeros or ASCII text, something is wrong with your input encoding.

Changing the last salt character from t to u changes every output byte unpredictably. This is the avalanche property that makes precomputed tables useless.

Common mistakes

A few errors show up again and again.

  • Reusing one salt for every user. The salt must be unique per password, ideally 16 random bytes from a CSPRNG. A shared salt lets an attacker build one table and attack every account at once. Store the salt next to the hash; it is not secret.
  • Iteration counts that are too low. OWASP recommends at least 600000 iterations for PBKDF2-HMAC-SHA256 as of 2023. A value of 1000 was fine in 2000 and is broken now. This tool defaults to 600000 for that reason.
  • Skipping the HKDF salt or info. HKDF works without them, but a per-context info label prevents two features from accidentally deriving the same key. Use a distinct string like app v1 encryption.
  • Requesting more HKDF output than the hash allows. Expand can produce at most 255 \times H bytes, where H is the hash length. For SHA-256 that is 8160 bytes. Ask for less.
  • Treating the derived key as a password. The output is a key. Do not display it to users or send it over an insecure channel.

Related tools

Once you have a key, feed it to AES Encryption to encrypt text with AES-GCM. Use a 32-byte PBKDF2 or HKDF output as the AES-256 key. To generate the salts and random master secrets these functions expect, use the Cryptographic Key Generator, which draws from a CSPRNG rather than a predictable source.

Frequently asked questions

Is PBKDF2 still safe in 2024?

Yes, with a high iteration count. PBKDF2 is FIPS-approved and widely deployed. It has one weakness: it is not memory-hard, so custom hardware attacks it more cheaply than they attack Argon2 or scrypt. If you have a free choice and no FIPS requirement, Argon2id is the stronger option. If you must use what browsers expose natively, PBKDF2 at 600000 iterations is a defensible choice.

Why does HKDF have both a salt and info?

The salt goes into the extract step and helps distribute entropy; it can be empty or a fixed value and still be safe because the input is already random. The info goes into the expand step and separates keys by context. They serve different stages, which is why both exist.

Can I use SHA-1 with these?

The tool offers it for compatibility with old systems, but avoid it for new work. SHA-1 is broken for collision resistance. HMAC-SHA1 is not directly broken, yet SHA-256 has no downside here and is the safer default.

Do I need to store the derived key?

No. Store the salt (and for PBKDF2, the iteration count and hash), then re-derive the key on demand from the same password. That is the whole design: the key lives only in memory when needed.

Why do the same inputs always give the same output?

Both functions are deterministic. Given identical password or IKM, salt, hash, iterations or info, and length, the output never changes. That is what lets you verify a password: derive again and compare. Randomness enters only through a unique salt, not through the function itself.