<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.random-bytes.php',
    1 => 'random_bytes',
    2 => 'R&eacute;cup&egrave;re des octets al&eacute;atoires cryptographiquement s&eacute;curis&eacute;s',
  ),
  'up' => 
  array (
    0 => 'ref.random.php',
    1 => 'Random Fonctions',
  ),
  'prev' => 
  array (
    0 => 'function.rand.php',
    1 => 'rand',
  ),
  'next' => 
  array (
    0 => 'function.random-int.php',
    1 => 'random_int',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Récupère des octets aléatoires cryptographiquement sécurisés</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="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">
   Génère une chaîne contenant des octets aléatoires uniformément sélectionnés avec la valeur de <code class="parameter">length</code>.
  </p>
  <p class="para">
   Les octets retournés étant choisis de manière totalement aléatoire, la chaîne résultante contiendra probablement
   des caractères non imprimables ou des séquences UTF-8 invalides. Il peut être nécessaire
   de l&#039;encoder avant la transmission ou l&#039;affichage.
  </p>
  <p class="para">
   La randomisation générée par cette fonction est adaptée à toutes les applications, y compris
   la génération de secrets à long terme, tels que des clés de chiffrement.
  </p>
  
<p class="para">
Les sources de hasard par ordre de priorité sont les suivantes :
</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 si CCRandomGenerateBytes est disponible au moment de la compilation): 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">
      Toute combinaison d&#039;un système d&#039;exploitation et d&#039;une version de PHP non mentionnée précédemment:
      <var class="filename">/dev/urandom</var>.
    </span>
</li>
<li class="listitem">
    <span class="simpara">
      Si aucune des sources de hasard n&#039;est disponible ou si elles échouent toutes à générer
      de l&#039;aléatoire, une exception de type <span class="classname"><a href="class.random-randomexception.php" class="classname">Random\RandomException</a></span>
      sera levée.
    </span>
</li>
</ul>

  
<blockquote class="note"><p><strong class="note">Note</strong>: 
<span class="simpara">
    Bien que cette fonction ait été ajoutée en PHP 7.0, une
    <a href="https://github.com/paragonie/random_compat" class="link external">&raquo;&nbsp;implementation en espace utilisateur</a>
    est disponible pour PHP 5.2 jusqu&#039;à 5.6, inclus.
</span>
</p></blockquote>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.random-bytes-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <dl>
   
    <dt><code class="parameter">length</code></dt>
    <dd>
     <p class="para">
      La longueur de la <a href="language.types.string.php" class="link">chaîne de caractères</a> aléatoire qui doit être retournée en octets; doit être supérieure ou égale à <code class="literal">1</code>.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.random-bytes-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne une <a href="language.types.string.php" class="link">chaîne de caractères</a> contenant le nombre demandé d&#039;octets cryptographiquement sécurisés.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.random-bytes-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <ul class="itemizedlist">
   
<li class="listitem">
<span class="simpara">
    Si aucune source de données aléatoires n&#039;est trouvée, une
    <span class="classname"><a href="class.random-randomexception.php" class="classname">Random\RandomException</a></span> sera lancée.
</span>
</li>

   <li class="listitem">
    <span class="simpara">
     Si la valeur de <code class="parameter">length</code> est inférieure à <code class="literal">1</code>,
     une <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> sera lancée.
    </span>
   </li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.random-bytes-changelog">
  <h3 class="title">Historique</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.2.0</td>
      <td>
       En cas d&#039;échec <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr>, cette fonction lancera
       désormais une <span class="classname"><a href="class.random-randomexception.php" class="classname">Random\RandomException</a></span>.
       Auparavant une <span class="classname"><a href="class.exception.php" class="classname">Exception</a></span> basique était lancée.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.random-bytes-examples">
  <h3 class="title">Exemples</h3>
  <div class="example" id="random-bytes.example.basic">
   <p><strong>Exemple #1 Exemple avec <span class="function"><strong>random_bytes()</strong></span></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>Résultat de l&#039;exemple ci-dessus est similaire à :</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">Voir aussi</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="random-randomizer.getbytes.php" class="function" rel="rdfs-seeAlso">Random\Randomizer::getBytes()</a> - Renvoie des octets al&eacute;atoires</span></li>
   <li><span class="function"><a href="function.random-int.php" class="function" rel="rdfs-seeAlso">random_int()</a> - R&eacute;cup&egrave;re un entier s&eacute;lectionn&eacute; de mani&egrave;re uniforme et cryptographiquement s&eacute;curis&eacute;e</span></li>
   <li><span class="function"><a href="function.bin2hex.php" class="function" rel="rdfs-seeAlso">bin2hex()</a> - Convertit des donn&eacute;es binaires en repr&eacute;sentation hexad&eacute;cimale</span></li>
   <li><span class="function"><a href="function.base64-encode.php" class="function" rel="rdfs-seeAlso">base64_encode()</a> - Encode une cha&icirc;ne en MIME base64</span></li>
  </ul>
 </div>


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