<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.random.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.random-bytes.php',
    1 => 'random_bytes',
    2 => 'Get cryptographically secure random bytes',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.rand.php',
    1 => 'rand',
  ),
  'next' => 
  array (
    0 => 'function.random-int.php',
    1 => 'random_int',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/random/functions/random-bytes.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.random-bytes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">random_bytes</h1>
  <p class="verinfo">(PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">random_bytes</span> &mdash; <span class="dc-title">Get cryptographically secure random bytes</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.random-bytes-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>random_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="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Generates a string containing uniformly selected random bytes with the requested <code class="parameter">length</code>.
  </p>
  <p class="para">
   As the returned bytes are selected completely randomly, the resulting string is likely
   to contain unprintable characters or invalid UTF-8 sequences. It may be necessary to
   encode it before transmission or display.
  </p>
  <p class="para">
   The randomness generated by this function is suitable for all applications, including
   the generation of long-term secrets, such as encryption keys.
  </p>
  
<p class="para">
 The sources of randomness in the order of priority are as follows:
</p>
<ul class="itemizedlist">
 <li class="listitem">
  <p class="para">
   Linux: <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">&raquo;&nbsp;getrandom()</a>, <var class="filename">/dev/urandom</var>
  </p>
 </li>
 <li class="listitem">
  <p class="para">
   FreeBSD &gt;= 12 (PHP &gt;= 7.3): <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">&raquo;&nbsp;getrandom()</a>, <var class="filename">/dev/urandom</var>
  </p>
 </li>
 <li class="listitem">
  <p class="para">
   Windows (PHP &gt;= 7.2): <a href="https://docs.microsoft.com/en-us/windows/desktop/SecCNG/cng-portal" class="link external">&raquo;&nbsp;CNG-API</a>
  </p>
  <p class="para">
   Windows: <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx" class="link external">&raquo;&nbsp;CryptGenRandom</a>
  </p>
 </li>
 <li class="listitem">
  <p class="para">
   macOS (PHP &gt;= 8.2; &gt;= 8.1.9; &gt;= 8.0.22 if CCRandomGenerateBytes is available at compile time): CCRandomGenerateBytes()
  </p>
  <p class="para">
   macOS (PHP &gt;= 8.1; &gt;= 8.0.2): arc4random_buf(), <var class="filename">/dev/urandom</var>
  </p>
 </li>
 <li class="listitem">
  <p class="para">
   NetBSD &gt;= 7 (PHP &gt;= 7.1; &gt;= 7.0.1): arc4random_buf(), <var class="filename">/dev/urandom</var>
  </p>
 </li>
 <li class="listitem">
  <p class="para">
   OpenBSD &gt;= 5.5 (PHP &gt;= 7.1; &gt;= 7.0.1): arc4random_buf(), <var class="filename">/dev/urandom</var>
  </p>
 </li>
 <li class="listitem">
  <p class="para">
   DragonflyBSD (PHP &gt;= 8.1): <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">&raquo;&nbsp;getrandom()</a>, <var class="filename">/dev/urandom</var>
  </p>
 </li>
 <li class="listitem">
  <p class="para">
   Solaris (PHP &gt;= 8.1): <a href="http://man7.org/linux/man-pages/man2/getrandom.2.html" class="link external">&raquo;&nbsp;getrandom()</a>, <var class="filename">/dev/urandom</var>
  </p>
 </li>
 <li class="listitem">
  <span class="simpara">
   Any combination of operating system and PHP version not previously mentioned: <var class="filename">/dev/urandom</var>
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   If none of the sources are available or they all fail to generate
   randomness, then a <span class="classname"><a href="class.random-randomexception.php" class="classname">Random\RandomException</a></span>
   will be thrown.
  </span>
 </li>
</ul>

  
 <blockquote class="note"><p><strong class="note">Nota</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">Elenco dei parametri</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; must be <code class="literal">1</code> or greater.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.random-bytes-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   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">Errori/Eccezioni</h3>
  <ul class="itemizedlist">
   
 <li class="listitem">
  <span class="simpara">
   If an appropriate source of randomness cannot be found,
   a <span class="classname"><a href="class.random-randomexception.php" class="classname">Random\RandomException</a></span> will be thrown.
  </span>
 </li>

   <li class="listitem">
    <span class="simpara">
     If the value of <code class="parameter">length</code> is less than <code class="literal">1</code>,
     a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> will be thrown.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.random-bytes-changelog">
  <h3 class="title">Log delle modifiche</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Versione</th>
      <th>Descrizione</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.2.0</td>
      <td>
       In case of a <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> failure, this function will now throw a
       <span class="classname"><a href="class.random-randomexception.php" class="classname">Random\RandomException</a></span>. Previously a plain
       <span class="classname"><a href="class.exception.php" class="classname">Exception</a></span> was thrown.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.random-bytes-examples">
  <h3 class="title">Esempi</h3>
  <div class="example" id="random-bytes.example.basic">
   <p><strong>Example #1 <span class="function"><strong>random_bytes()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$bytes </span><span style="color: #007700">= </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>Il precedente esempio visualizzerà
qualcosa simile a:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">string(10) &quot;385e33f741&quot;</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.random-bytes-seealso">
  <h3 class="title">Vedere anche:</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="random-randomizer.getbytes.php" class="function" rel="rdfs-seeAlso">Random\Randomizer::getBytes()</a> - Get random bytes</span></li>
   <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - Get a cryptographically secure, uniformly selected integer</span></li>
   <li><span class="function"><a href="function.bin2hex.php" class="function" rel="rdfs-seeAlso">bin2hex()</a> - Converte i dati binari nella rappresentazione esadecimale</span></li>
   <li><span class="function"><a href="function.base64-encode.php" class="function" rel="rdfs-seeAlso">base64_encode()</a> - Encodes data with MIME base64</span></li>
  </ul>
 </div>


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