PHP hash_pbkdf2() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Hash

Definition

In PHP, hash_pbkdf2() turns a password and a salt into a key by running a hash many times (PBKDF2).

The many rounds make each guess slow, so it is good for deriving encryption keys from passwords.

Syntax

Call hash_pbkdf2() with the values you need. Start with this line.

PHP
$key = hash_pbkdf2('sha256', $password, $salt, 600000, 64);
CallWhat it does
hash_pbkdf2('sha256', 'pw', 'salt', 1000)Returns the full-length hex key.
hash_pbkdf2('sha256', 'pw', 'salt', 1000, 20)Returns a key 20 hex characters long.

Use at least 600,000 iterations with sha256 in real apps. Small numbers are used here so the examples run fast.

Library declarations
PHP
hash_pbkdf2(string $algo, string $password, string $salt, int $iterations, int $length = 0, bool $binary = false, array $options = []): string

Parameters

ParameterDescription
algoRequired. A cryptographic algorithm such as "sha256".
passwordRequired. The password to derive from.
saltRequired. A random salt. Use at least 16 random bytes.
iterationsRequired. The number of rounds. Higher is slower and safer.
lengthOptional. Length of the result. 0 (default) uses the full hash size. For hex output it counts hex characters.
binaryOptional. false (default) returns hex. true returns raw bytes.
optionsOptional. Reserved for future use.

Return Value

Return ValueDescription
stringThe derived key as hex, or raw bytes when binary is true. A bad algorithm or zero iterations throws ValueError.

PHP Examples

Example 1 — Derive a Key from a Password

A fixed salt and iteration count give a repeatable key. Real code uses a random salt and many more rounds.

PHP
<?php
$key = hash_pbkdf2('sha256', 'password', 'salt-value', 1000, 0);

echo $key, PHP_EOL;

How It Works

  1. Pass the password and salt. The salt makes keys unique.
  2. Set the rounds. 1000 is for demo speed only.
  3. Use length 0. You get the full 64 hex characters.
  4. Run again. The same inputs give the same key.

Example 2 — Shorter Output

Set the length in hex characters.

PHP
<?php
$key = hash_pbkdf2('sha256', 'password', 'salt-value', 1000, 20);

echo strlen($key), PHP_EOL;
echo $key, PHP_EOL;

How It Works

  1. Pass 20 as the length. It counts hex characters.
  2. Get a short key. Only 20 characters come back.
  3. Check with `strlen()`. It prints 20.
  4. Know the limit. Shorter keys are weaker.

Example 3 — Check a Password Against a Saved Key

Store the salt and key, then derive again at login.

PHP
<?php
function derive(string $password, string $salt): string
{
    return hash_pbkdf2('sha256', $password, $salt, 1000, 64);
}

$salt   = 'demo-salt-01';
$stored = derive('S3cret!pass', $salt);

foreach (['S3cret!pass', 'guess'] as $typed) {
    echo $typed, ": ", hash_equals($stored, derive($typed, $salt)) ? "ok" : "wrong", PHP_EOL;
}

How It Works

  1. Derive and save a key. Keep the salt with it.
  2. Derive again at login. Use the same salt and rounds.
  3. Compare with `hash_equals()`. It is timing-safe.
  4. Prefer `password_hash()` for real logins.

Frequently Asked Questions

It derives a key from a password and a salt by repeating an HMAC many times. The repeats make brute-force guessing slow.
hash_pbkdf2(string $algo, string $password, string $salt, int $iterations, int $length = 0, bool $binary = false, array $options = []): string.
At least 600,000 with sha256 and 210,000 with sha512, following current OWASP advice. Raise the number as hardware gets faster.
No. password_hash() is the best choice for logins. Use hash_pbkdf2() to derive keys or when a standard requires PBKDF2.
With hex output (the default) length counts hex characters, not bytes. With binary output it counts bytes.

Next: hash_update()

hash_update() adds data to an incremental hash.

hash_update() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful