<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.openssl-pbkdf2.php',
    1 => 'openssl_pbkdf2',
    2 => 'Generates a PKCS5 v2 PBKDF2 string',
  ),
  'up' => 
  array (
    0 => 'ref.openssl.php',
    1 => 'OpenSSL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.openssl-password-verify.php',
    1 => 'openssl_password_verify',
  ),
  'next' => 
  array (
    0 => 'function.openssl-pkcs12-export.php',
    1 => 'openssl_pkcs12_export',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/openssl/functions/openssl-pbkdf2.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.openssl-pbkdf2" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_pbkdf2</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">openssl_pbkdf2</span> &mdash; <span class="dc-title">Generates a PKCS5 v2 PBKDF2 string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.openssl-pbkdf2-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_pbkdf2</strong></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">$key_length</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.string.php" class="type string">string</a></span> <code class="parameter">$digest_algo</code><span class="initializer"> = &quot;sha1&quot;</span></span><br>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>openssl_pbkdf2()</strong></span> computes PBKDF2 (Password-Based Key Derivation Function 2),
   a key derivation function defined in PKCS5 v2.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-pbkdf2-parameters">
  <h3 class="title">Parameters</h3>
  <dl>
   
    <dt><code class="parameter">password</code></dt>
    <dd>
     <p class="para">
      Password from which the derived key is generated.
     </p>
    </dd>
   
   
    <dt><code class="parameter">salt</code></dt>
    <dd>
     <p class="para">
      PBKDF2 recommends a crytographic salt of at least 128 bits (16 bytes).
     </p>
    </dd>
   
   
    <dt><code class="parameter">key_length</code></dt>
    <dd>
     <p class="para">
      Length of desired output key.
     </p>
    </dd>
   
   
    <dt><code class="parameter">iterations</code></dt>
    <dd>
     <p class="para">
      The number of iterations desired.
      <a href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf" class="link external">&raquo;&nbsp;NIST
       recommends at least 1,000</a>. As of 2023, OWASP recommends 600,000 iterations for
      PBKDF2-HMAC-SHA256 and 210,000 for PBKDF2-HMAC-SHA512.
     </p>
    </dd>
   
   
    <dt><code class="parameter">digest_algo</code></dt>
    <dd>
     <p class="para">
      Optional hash or digest algorithm from <span class="function"><a href="function.openssl-get-md-methods.php" class="function">openssl_get_md_methods()</a></span>.  Defaults
      to SHA-1. It is recommended to set it to SHA-256 or SHA-512.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-pbkdf2-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns raw binary string or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.openssl-pbkdf2-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 openssl_pbkdf2() example</strong></p>
    <div class="example-contents">
<div class="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">$salt </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">16</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$keyLength </span><span style="color: #007700">= </span><span style="color: #0000BB">20</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 /></span><span style="color: #0000BB">$generated_key </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_pbkdf2</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">$keyLength</span><span style="color: #007700">, </span><span style="color: #0000BB">$iterations</span><span style="color: #007700">, </span><span style="color: #DD0000">'sha256'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$generated_key</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">base64_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$generated_key</span><span style="color: #007700">).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>


   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-pbkdf2-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.hash-pbkdf2.php" class="function" rel="rdfs-seeAlso">hash_pbkdf2()</a> - Generate a PBKDF2 key derivation of a supplied password</span></li>
    <li><span class="function"><a href="function.openssl-get-md-methods.php" class="function" rel="rdfs-seeAlso">openssl_get_md_methods()</a> - Gets available digest methods</span></li>
   </ul>
  </p>
 </div>


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