array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), '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' => 'ja', '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 — 疑似ランダムなバイト文字列を生成する
疑似ランダムなバイト文字列を生成します。長さは
length
パラメータで指定します。
暗号学的に強いアルゴリズムを使って疑似乱数を生成したかどうかを知ることもできます。
オプションのパラメータ strong_result
を使います。
これが false
になることはまずないでしょうが、
古いシステムなどではそうなることもあります。
成功した場合は指定したバイト長の string を返します。
openssl_random_pseudo_bytes() は、 失敗した場合に Exception をスローします。
バージョン | 説明 |
---|---|
8.0.0 |
strong_result は、nullable になりました。
|
7.4.0 |
この関数は、失敗時に false を返さなくなりました。
代わりに 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)