PHPマニュアル/PEARマニュアル | ユーザフォーラムで議論/質問 | マニュアル検索 | ハイライト | ハイライトオフ | ポータル | php spot

random_bytes - Generates cryptographically secure pseudo-random bytes | JavaScript入門&応用&リファレンスなら「JavaScriptist」

  

random_bytes

(PHP 7)

random_bytesGenerates cryptographically secure pseudo-random bytes

説明

string random_bytes ( int $length )

Generates an arbitrary length string of cryptographic random bytes that are suitable for cryptographic use, such as when generating salts, keys or initialization vectors.

この関数が使う不規則性のソースはつぎのとおりです。

  • Windows では、常に » CryptGenRandom() を使います。
  • Linux では、システムコール » getrandom(2) があればそれを使います。
  • その他のプラットフォームでは、 /dev/urandom を使います。
  • これらがいずれも使えない場合は、 Exception をスローします。

注意: この関数は PHP 7.0 で追加されたものですが、PHP 5.2 から 5.6 までのバージョンで使える » ユーザーランドの実装 も公開されています。

パラメータ

length

The length of the random string that should be returned in bytes.

返り値

Returns a string containing the requested number of cryptographically secure random bytes.

エラー / 例外

  • 適切な不規則性のソースが見つからない場合は Exception をスローします。
  • 無効なパラメータが指定された場合は TypeError をスローします。
  • If an invalid length of bytes is given, an Error will be thrown.

例1 random_bytes() example

<?php
$bytes 
random_bytes(5);
var_dump(bin2hex($bytes));
?>

上の例の出力は、 たとえば以下のようになります。

string(10) "385e33f741"

参考


忘却曲線を使ってこの関数を確実に記憶に残す

フォーラムで「random_bytes - Generates cryptographically secure pseudo-random bytes」について話す
各種マニュアル: PHPマニュアル | PEARマニュアル | Smarty(英語)マニュアル | PHP-GTKマニュアル | random_bytes - Generates cryptographically secure pseudo-random bytes」をGoogle検索
copyright © 1997-2024 PHP ドキュメント作成グループ(ライセンス). provided by php spot. マニュアル: