array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.openssl-random-pseudo-bytes.php', 1 => 'openssl_random_pseudo_bytes', ), 'up' => array ( 0 => 'ref.openssl.php', 1 => 'OpenSSL 函数', ), 'prev' => array ( 0 => 'function.openssl-public-encrypt.php', 1 => 'openssl_public_encrypt', ), 'next' => array ( 0 => 'function.openssl-seal.php', 1 => 'openssl_seal', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/openssl/functions/openssl-random-pseudo-bytes.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
openssl_random_pseudo_bytes — 生成一个伪随机字节串
生成一个伪随机字节串 string ,字节数由 length
参数指定。
通过 strong_result
参数可以表示在生成随机字节的过程中是否使用了强加密算法。返回值为 false
的情况很少见,但已损坏或老化的有些系统上会出现。
返回生成的字节 string。
openssl_random_pseudo_bytes() 失败时抛出 Exception。
示例 #1 openssl_random_pseudo_bytes() 示例
<?php
for ($i = 1; $i <= 4; $i++) {
$bytes = openssl_random_pseudo_bytes($i, $cstrong);
$hex = bin2hex($bytes);
echo "Lengths: Bytes: $i and Hex: " . strlen($hex) . PHP_EOL;
var_dump($hex);
var_dump($cstrong);
echo PHP_EOL;
}
?>
以上示例的输出类似于:
Lengths: Bytes: 1 and Hex: 2 string(2) "42" bool(true) Lengths: Bytes: 2 and Hex: 4 string(4) "dc6e" bool(true) Lengths: Bytes: 3 and Hex: 6 string(6) "288591" bool(true) Lengths: Bytes: 4 and Hex: 8 string(8) "ab86d144" bool(true)