Sophie

Sophie

distrib > Mageia > 7 > aarch64 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 6063

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Generates cryptographically secure pseudo-random bytes</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.csprng.html">CSPRNG Functions</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.random-int.html">random_int</a></div>
 <div class="up"><a href="ref.csprng.html">CSPRNG Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.random-bytes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">random_bytes</h1>
  <p class="verinfo">(PHP 7)</p><p class="refpurpose"><span class="refname">random_bytes</span> &mdash; <span class="dc-title">Generates cryptographically secure pseudo-random bytes</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.random-bytes-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>random_bytes</strong></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
   )</div>

  <p class="para rdfs-comment">
   Generates an arbitrary length string of cryptographic random bytes that are
   suitable for cryptographic use, such as when generating salts, keys or
   initialization vectors.
  </p>
  
<p class="para">
 The sources of randomness used for this function are as follows:
</p>
<ul class="itemizedlist">
 <li class="listitem">
  <span class="simpara">
   On Windows,
   <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx" class="link external">&raquo;&nbsp;<span class="function"><strong>CryptGenRandom()</strong></span></a>
   will always be used.
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   On Linux, the
   <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">&raquo;&nbsp;getrandom(2)</a>
   syscall will be used if available.
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   On other platforms, <var class="filename">/dev/urandom</var> will be used.
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   If none of the aforementioned sources are available, then an
   <a href="class.exception.html" class="classname">Exception</a> will be thrown.
  </span>
 </li>
</ul>

  
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <span class="simpara">
   Although this function was added to PHP in PHP 7.0, a
   <a href="https://github.com/paragonie/random_compat" class="link external">&raquo;&nbsp;userland implementation</a>
   is available for PHP 5.2 to 5.6, inclusive.
  </span>
 </p></blockquote>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.random-bytes-parameters">
  <h3 class="title">Parameters</h3>
  <dl>

   
    <dt>
<code class="parameter">length</code></dt>

    <dd>

     <p class="para">
      The length of the random string that should be returned in bytes.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.random-bytes-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns a string containing the requested number of cryptographically
   secure random bytes.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.random-bytes-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <ul class="itemizedlist">
   
 <li class="listitem">
  <span class="simpara">
   If an appropriate source of randomness cannot be found,
   an <a href="class.exception.html" class="classname">Exception</a> will be thrown.
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
    If invalid parameters are given, a <a href="class.typeerror.html" class="classname">TypeError</a>
    will be thrown.
  </span>
 </li>

   <li class="listitem">
    <span class="simpara">
     If an invalid <code class="parameter">length</code> of bytes is given, an
     <a href="class.error.html" class="classname">Error</a> will be thrown.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-function.random-bytes-examples">
  <h3 class="title">Examples</h3>
  <div class="example" id="example-919">
   <p><strong>Example #1 <span class="function"><strong>random_bytes()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</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 /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(10) &quot;385e33f741&quot;
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.random-bytes-seealso">
  <h3 class="title">See Also</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.random-int.html" class="function" rel="rdfs-seeAlso">random_int()</a> - Generates cryptographically secure pseudo-random integers</span></li>
   <li class="member"><span class="function"><a href="function.openssl-random-pseudo-bytes.html" class="function" rel="rdfs-seeAlso">openssl_random_pseudo_bytes()</a> - Generate a pseudo-random string of bytes</span></li>
   <li class="member"><span class="function"><a href="function.bin2hex.html" class="function" rel="rdfs-seeAlso">bin2hex()</a> - Convert binary data into hexadecimal representation</span></li>
  </ul>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ref.csprng.html">CSPRNG Functions</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.random-int.html">random_int</a></div>
 <div class="up"><a href="ref.csprng.html">CSPRNG Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>