hash_pbkdf2

Generate a PBKDF2 key derivation of a supplied password

Syntax

hash_pbkdf2(
    string $algo,
    string $password,
    string $salt,
    int $iterations,
    int $length = 0,
    bool $binary = false
): string

Parameters

algo

Name of selected hashing algorithm.

password

The password to use for the derivation.

salt

The salt to use for the derivation. This value should be generated randomly.

iterations

The number of internal iterations to perform for the derivation.

length

The length of the output string. If binary is true this corresponds to the byte-length of the derived key, if binary is false this corresponds to twice the byte-length of the derived key (as every byte of the key is returned as two hexits).

If 0 is passed, the entire output of the supplied algorithm is used.

binary

When set to true, outputs raw binary data. false outputs lowercase hexits.

Return

Returns a string containing the derived key as lowercase hexits unless binary is set to true in which case the raw binary representation of the derived key is returned.

Examples

1 · algo password salt iterations

<?

$algo = 'sha384';
$password = 'password';
$salt = random_bytes(16);
$iterations = 1000;

$return = hash_pbkdf2($algo, $password, $salt, $iterations);

echo $return;

?>
183fedcd29fe7f74e4402183c673506ae02ed1cdf57a5c1403213d910a3e0cfabf921738436787c143e1a1341968e90f

2 · length

<?

$algo = 'sha384';
$password = 'password';
$salt = random_bytes(16);
$iterations = 1000;
$length = 32;

$return = hash_pbkdf2($algo, $password, $salt, $iterations, $length);

echo $return;

?>
a471b3c89f96e73090c812ed7d7fd0cb

3 · binary

<?

$algo = 'sha384';
$password = 'password';
$salt = random_bytes(16);
$iterations = 1000;
$length = 32;
$binary = true;

$return = hash_pbkdf2($algo, $password, $salt, $iterations, $length, $binary);

echo $return . PHP_EOL . bin2hex($return);

?>
�3T�'p��:�/�P�(<j�tsb�8L{,^a
ea3354882770148cd53ac2112fe45089283c6ab574736290384c7b0c2c1b5e61
HomeMenu