<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.openssl.inc";
$setup = array (
  'home' => 
  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',
    2 => '疑似ランダムなバイト文字列を生成する',
  ),
  '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',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.openssl-random-pseudo-bytes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_random_pseudo_bytes</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_random_pseudo_bytes</span> &mdash; <span class="dc-title">疑似ランダムなバイト文字列を生成する</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-random-pseudo-bytes-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_random_pseudo_bytes</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$length</code></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter reference">&$strong_result</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   疑似ランダムなバイト文字列を生成します。長さは
   <code class="parameter">length</code> パラメータで指定します。
  </p>
  <p class="para">
   暗号学的に強いアルゴリズムを使って疑似乱数を生成したかどうかを知ることもできます。
   オプションのパラメータ <code class="parameter">strong_result</code> を使います。
   これが <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> になることはまずないでしょうが、
   古いシステムなどではそうなることもあります。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-random-pseudo-bytes-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       希望するバイト長。
       <code class="literal">2147483647</code> 以下の正の整数でなければなりません。
       PHP は、
       このパラメータを非 null の整数値にキャストして利用します。
      </p>
     </dd>
    
    
     <dt><code class="parameter">strong_result</code></dt>
     <dd>
      <p class="para">
       これを渡しておくと、使ったアルゴリズムが暗号学的に強いものであるかどうかを表す
       <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> 値が格納されます。「強い」とは、
       GPG やパスワードなどに使っても安全であるという意味です。強い場合は
       <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>、そうでない場合は <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> となります。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-random-pseudo-bytes-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   成功した場合は指定したバイト長の <span class="type"><a href="language.types.string.php" class="type string">string</a></span> を返します。
  </p>
 </div>

 
 <div class="refsect1 errors" id="refsect1-function.openssl-random-pseudo-bytes-errors">
  <h3 class="title">エラー / 例外</h3>
  <p class="para">
   <span class="function"><strong>openssl_random_pseudo_bytes()</strong></span> は、
   失敗した場合に <span class="classname"><a href="class.exception.php" class="classname">Exception</a></span> をスローします。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.openssl-random-pseudo-bytes-changelog">
  <h3 class="title">変更履歴</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>バージョン</th>
      <th>説明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">strong_result</code> は、nullable になりました。
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       この関数は、失敗時に <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返さなくなりました。
       代わりに <span class="classname"><a href="class.exception.php" class="classname">Exception</a></span>
       をスローするようになっています。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>

 <div class="refsect1 examples" id="refsect1-function.openssl-random-pseudo-bytes-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 <span class="function"><strong>openssl_random_pseudo_bytes()</strong></span> の例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt;= </span><span style="color: #0000BB">4</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br />    </span><span style="color: #0000BB">$bytes </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">, </span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">$hex   </span><span style="color: #007700">= </span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">);<br /><br />    echo </span><span style="color: #DD0000">"Lengths: Bytes: </span><span style="color: #0000BB">$i</span><span style="color: #DD0000"> and Hex: " </span><span style="color: #007700">. </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">) . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br />    echo </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Lengths: Bytes: 1 and Hex: 2
string(2) &quot;42&quot;
bool(true)

Lengths: Bytes: 2 and Hex: 4
string(4) &quot;dc6e&quot;
bool(true)

Lengths: Bytes: 3 and Hex: 6
string(6) &quot;288591&quot;
bool(true)

Lengths: Bytes: 4 and Hex: 8
string(8) &quot;ab86d144&quot;
bool(true)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-random-pseudo-bytes-seealso">
  <h3 class="title">参考</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.random-bytes.php" class="function" rel="rdfs-seeAlso">random_bytes()</a> - 暗号学的にセキュアな、ランダムなバイト列を生成する</span></li>
   <li><span class="function"><a href="function.bin2hex.php" class="function" rel="rdfs-seeAlso">bin2hex()</a> - バイナリのデータを16進表現に変換する</span></li>
   <li><span class="function"><a href="function.crypt.php" class="function" rel="rdfs-seeAlso">crypt()</a> - 文字列の一方向のハッシュ化を行う</span></li>
   <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - 暗号学的にセキュアな方法で、等確率に出る整数を取得する</span></li>
  </ul>
 </div>

</div><?php manual_footer($setup); ?>