<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.hash.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'it',
  ),
  'this' => 
  array (
    0 => 'function.hash-pbkdf2.php',
    1 => 'hash_pbkdf2',
    2 => 'Generate a PBKDF2 key derivation of a supplied password',
  ),
  'up' => 
  array (
    0 => 'ref.hash.php',
    1 => 'Hash Funzioni',
  ),
  'prev' => 
  array (
    0 => 'function.hash-init.php',
    1 => 'hash_init',
  ),
  'next' => 
  array (
    0 => 'function.hash-update.php',
    1 => 'hash_update',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/hash/functions/hash-pbkdf2.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.hash-pbkdf2" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">hash_pbkdf2</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">hash_pbkdf2</span> &mdash; <span class="dc-title">Generate a PBKDF2 key derivation of a supplied password</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.hash-pbkdf2-description">
  <h3 class="title">Descrizione</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>hash_pbkdf2</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$algo</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="attribute"><a href="class.sensitiveparameter.php">#[\SensitiveParameter]</a> </span><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$password</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$salt</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$iterations</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$length</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$binary</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$options</code><span class="initializer"> = []</span></span><br>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.hash-pbkdf2-parameters">
  <h3 class="title">Elenco dei parametri</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">algo</code></dt>
     <dd>
      <p class="para">
       Name of selected hashing algorithm (e.g. <code class="literal">&quot;sha256&quot;</code>).
       For a list of supported algorithms see <span class="function"><a href="function.hash-hmac-algos.php" class="function">hash_hmac_algos()</a></span>.
       <blockquote class="note"><p><strong class="note">Nota</strong>: 
        <p class="para">
         Non-cryptographic hash functions are not allowed.
        </p>
       </p></blockquote>
      </p>
     </dd>
    
    
     <dt><code class="parameter">password</code></dt>
     <dd>
      <p class="para">
       The password to use for the derivation.
      </p>
     </dd>
    
    
     <dt><code class="parameter">salt</code></dt>
     <dd>
      <p class="para">
       The salt to use for the derivation. This value should be generated randomly.
      </p>
     </dd>
    
    
     <dt><code class="parameter">iterations</code></dt>
     <dd>
      <p class="para">
       The number of internal iterations to perform for the derivation.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       The length of the output string. If <code class="parameter">binary</code>
       is <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> this corresponds to the byte-length of the derived key, if
       <code class="parameter">binary</code> is <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> this corresponds to twice the
       byte-length of the derived key (as every byte of the key is returned as
       two hexits).
      </p>
      <p class="para">
       If <code class="literal">0</code> is passed, the entire output of the supplied
       algorithm is used.
      </p>
     </dd>
    
    
     <dt><code class="parameter">binary</code></dt>
     <dd>
      <p class="para">
       When set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, outputs raw binary data. <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> outputs lowercase
       hexits.
      </p>
     </dd>
    
    
     <dt><code class="parameter">options</code></dt>
     <dd>
      <p class="para">
       An array of options for the various hashing algorithms.
       Currently, only the <code class="literal">&quot;seed&quot;</code> key is
       supported by the MurmurHash variants.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.hash-pbkdf2-returnvalues">
  <h3 class="title">Valori restituiti</h3>
  <p class="para">
   Returns a string containing the derived key as lowercase hexits unless
   <code class="parameter">binary</code> is set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> in which case the raw
   binary representation of the derived key is returned.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.hash-pbkdf2-errors">
  <h3 class="title">Errori/Eccezioni</h3>
  <p class="para">
   Throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> exception if the algorithm is
   unknown, the <code class="parameter">iterations</code> parameter is less than or
   equal to <code class="literal">0</code>, the <code class="parameter">length</code> is less
   than <code class="literal">0</code> or the <code class="parameter">salt</code> is too long
   (greater than <strong><code>INT_MAX</code></strong><code class="literal"> - 4</code>).
  </p>
 </div>


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

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        Now throws a <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> exception on error.
        Previously, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> was returned and an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
        message was emitted.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>Usage of non-cryptographic hash functions (adler32, crc32, crc32b, fnv132, fnv1a32, fnv164, fnv1a64, joaat) was disabled.</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.hash-pbkdf2-examples">
  <h3 class="title">Esempi</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>hash_pbkdf2()</strong></span> example, basic usage</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$password </span><span style="color: #007700">= </span><span style="color: #DD0000">"password"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$iterations </span><span style="color: #007700">= </span><span style="color: #0000BB">600000</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Generate a cryptographically secure random salt using random_bytes()<br /></span><span style="color: #0000BB">$salt </span><span style="color: #007700">= </span><span style="color: #0000BB">random_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">16</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$hash </span><span style="color: #007700">= </span><span style="color: #0000BB">hash_pbkdf2</span><span style="color: #007700">(</span><span style="color: #DD0000">"sha256"</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">$salt</span><span style="color: #007700">, </span><span style="color: #0000BB">$iterations</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$hash</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// for raw binary, the $length needs to be halved for equivalent results<br /></span><span style="color: #0000BB">$hash </span><span style="color: #007700">= </span><span style="color: #0000BB">hash_pbkdf2</span><span style="color: #007700">(</span><span style="color: #DD0000">"sha256"</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">, </span><span style="color: #0000BB">$salt</span><span style="color: #007700">, </span><span style="color: #0000BB">$iterations</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #0000BB">true</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">$hash</span><span style="color: #007700">));</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(20) &quot;120fb6cffcf8b32c43e7&quot;
string(20) &quot;120fb6cffcf8b32c43e7&quot;</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.hash-pbkdf2-notes">
  <h3 class="title">Note</h3>
  <div class="caution"><strong class="caution">Attenzione</strong>
   <p class="para">
    The PBKDF2 method can be used for hashing passwords for storage. However, it
    should be noted that <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> or
    <span class="function"><a href="function.crypt.php" class="function">crypt()</a></span> with <strong><code><a href="string.constants.php#constant.crypt-blowfish">CRYPT_BLOWFISH</a></code></strong> are
    better suited for password storage.
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.hash-pbkdf2-seealso">
  <h3 class="title">Vedere anche:</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.password-hash.php" class="function" rel="rdfs-seeAlso">password_hash()</a> - Creates a password hash</span></li>
    <li><span class="function"><a href="function.hash-hkdf.php" class="function" rel="rdfs-seeAlso">hash_hkdf()</a> - Generate a HKDF key derivation of a supplied key input</span></li>
    <li><span class="function"><a href="function.sodium-crypto-pwhash.php" class="function" rel="rdfs-seeAlso">sodium_crypto_pwhash()</a> - Derive a key from a password, using Argon2</span></li>
   </ul>
  </p>
 </div>


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