openssl_pbkdf2

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

openssl_pbkdf2生成一个 PKCS5 v2 PBKDF2 字符串

说明

openssl_pbkdf2(
    string $password,
    string $salt,
    int $key_length,
    int $iterations,
    string $digest_algo = "sha1"
): string|false

openssl_pbkdf2() 计算 PBKDF2 (Password-Based Key Derivation Function 2), 在PKCS5 v2中定义的一个密钥的推导函数.

参数

password

派生密钥所生成的密码。

salt

PBKDF2 推荐一个不少于64位(8字节)的密码盐值。

key_length

希望输出密钥的长度。

iterations

需要的迭代次数 » NIST 建议至少10,000次.

digest_algo

openssl_get_md_methods()中可选的散列或摘要算法.默认是 SHA-1.

返回值

成功,返回原始二进制字符串 或者在失败时返回 false.

示例

示例 #1 openssl_pbkdf2() 示例

<?php
$password
= 'password';
$salt = openssl_random_pseudo_bytes(16);
$keyLength = 20;
$iterations = 600000;
$generated_key = openssl_pbkdf2($password, $salt, $keyLength, $iterations, 'sha256');
echo
bin2hex($generated_key)."\n";
echo
base64_encode($generated_key)."\n";
?>

参见

add a note add a note

User Contributed Notes 1 note

up
4
McGlockenshire
10 years ago
Despite the manual claiming that this is available in PHP 5.5 and above, this function wasn't made available in my local install.

I expect that having a prehistoric OpenSSL library version installed is the likely culprit.

If you're using PHP 5.5 and don't have this function available in your OpenSSL extension, look at the functionally equivalent hash_pbkdf2 function instead.
To Top