<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.password.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.password-hash.php',
    1 => 'password_hash',
    2 => 'Creates a password hash',
  ),
  'up' => 
  array (
    0 => 'ref.password.php',
    1 => 'Функції Хешування Пароля',
  ),
  'prev' => 
  array (
    0 => 'function.password-get-info.php',
    1 => 'password_get_info',
  ),
  'next' => 
  array (
    0 => 'function.password-needs-rehash.php',
    1 => 'password_needs_rehash',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/password/functions/password-hash.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.password-hash" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">password_hash</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">password_hash</span> &mdash; <span class="dc-title">Creates a password hash</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.password-hash-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>password_hash</strong></span>(<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>, <span class="methodparam"><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.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$algo</code></span>, <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>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>password_hash()</strong></span> creates a new password hash using a strong one-way hashing
   algorithm.
  </p>
  <p class="simpara">
   The following algorithms are currently supported:
  </p>
  <p class="para">
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      <strong><code><a href="password.constants.php#constant.password-default">PASSWORD_DEFAULT</a></code></strong> - Use the bcrypt algorithm (default as of PHP 5.5.0).
      Note that this constant is designed to change over time as new and stronger algorithms are added
      to PHP. For that reason, the length of the result from using this identifier can change over
      time. Therefore, it is recommended to store the result in a database column that can expand
      beyond 60 bytes (255 bytes would be a good choice).
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <strong><code><a href="password.constants.php#constant.password-bcrypt">PASSWORD_BCRYPT</a></code></strong> - Use the bcrypt algorithm to
      create the hash. This will produce a standard <span class="function"><a href="function.crypt.php" class="function">crypt()</a></span> compatible hash using
      the <code class="literal">$2y$</code> identifier.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <strong><code><a href="password.constants.php#constant.password-argon2i">PASSWORD_ARGON2I</a></code></strong> - Use the Argon2i hashing algorithm to create the hash.
      This algorithm is only available if PHP has been compiled with Argon2 support.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      <strong><code><a href="password.constants.php#constant.password-argon2id">PASSWORD_ARGON2ID</a></code></strong> - Use the Argon2id hashing algorithm to create the hash.
      This algorithm is only available if PHP has been compiled with Argon2 support.
     </span>
    </li>
   </ul>
  </p>
  <p class="simpara">
   Supported options for <strong><code><a href="password.constants.php#constant.password-bcrypt">PASSWORD_BCRYPT</a></code></strong>:
  </p>
  <p class="para">
   <ul class="itemizedlist">
    <li class="listitem">
     <p class="para">
      <code class="literal">salt</code> (<span class="type"><a href="language.types.string.php" class="type string">string</a></span>) - to manually provide a salt to use when hashing the password.
      Note that this will override and prevent a salt from being automatically generated.
     </p>
     <p class="para">
      If omitted, a random salt will be generated by <span class="function"><strong>password_hash()</strong></span> for
      each password hashed. This is the intended mode of operation.
     </p>
     <div class="warning"><strong class="warning">Увага</strong>
      <p class="para">
       The salt option is deprecated. It is now
       preferred to simply use the salt that is generated by default.
       As of PHP 8.0.0, an explicitly given salt is ignored.
      </p>
     </div>
    </li>
    <li class="listitem">
     <p class="para">
      <code class="literal">cost</code> (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>) - which denotes the algorithmic cost that should be used. 
      Examples of these values can be found on the <span class="function"><a href="function.crypt.php" class="function">crypt()</a></span> page.
     </p>
     <p class="para">
      If omitted, a default value of <code class="literal">12</code> will be used. This is a good
      baseline cost, but it should be adjusted depending on hardware used.
     </p>
    </li>
   </ul>
  </p>
  <p class="simpara">
   Supported options for <strong><code><a href="password.constants.php#constant.password-argon2i">PASSWORD_ARGON2I</a></code></strong>
   and <strong><code><a href="password.constants.php#constant.password-argon2id">PASSWORD_ARGON2ID</a></code></strong>:
  </p>
  <p class="para">
   <ul class="itemizedlist">
    <li class="listitem">
     <p class="para">
      <code class="literal">memory_cost</code> (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>) - Maximum memory (in kibibytes) that may 
      be used to compute the Argon2 hash. Defaults to <strong><code><a href="password.constants.php#constant.password-argon2-default-memory-cost">PASSWORD_ARGON2_DEFAULT_MEMORY_COST</a></code></strong>.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <code class="literal">time_cost</code> (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>) - Maximum amount of time it may 
      take to compute the Argon2 hash. Defaults to <strong><code><a href="password.constants.php#constant.password-argon2-default-time-cost">PASSWORD_ARGON2_DEFAULT_TIME_COST</a></code></strong>.
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <code class="literal">threads</code> (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>) - Number of threads to use for computing 
      the Argon2 hash. Defaults to <strong><code><a href="password.constants.php#constant.password-argon2-default-threads">PASSWORD_ARGON2_DEFAULT_THREADS</a></code></strong>.
     </p>
     <div class="warning"><strong class="warning">Увага</strong>
      <p class="para">
        Only available when PHP uses libargon2, not with libsodium implementation.
      </p>
     </div>
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.password-hash-parameters">
  <h3 class="title">Параметри</h3>
  <dl>
   
    <dt><code class="parameter">password</code></dt>
    <dd>
     <p class="para">
      Користувацький пароль.
     </p>
      <div class="caution"><strong class="caution">Застереження</strong>
       <p class="para">
        Using the <strong><code><a href="password.constants.php#constant.password-bcrypt">PASSWORD_BCRYPT</a></code></strong> as the
        algorithm, will result
        in the <code class="parameter">password</code> parameter being truncated to a
        maximum length of 72 bytes.
       </p>
      </div>
    </dd>
   
   
    <dt><code class="parameter">algo</code></dt>
    <dd>
     <p class="para">
      <a href="password.constants.php" class="link">Константа алгоритму для паролю</a>, що позначає алгоритм для гешування пароля.
     </p>
    </dd>
   
   
    <dt><code class="parameter">options</code></dt>
    <dd>
     <p class="para">
      Асоціативниий масив опцій. Документація щодо доступних опцій для кожного алгоритму викладена на сторінці <a href="password.constants.php" class="link">Константи алгоритмів для паролів</a>.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.password-hash-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Returns the hashed password.
  </p>
  <p class="para">
   The used algorithm, cost and salt are returned as part of the hash. Therefore,
   all information that&#039;s needed to verify the hash is included in it. This allows
   the <span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span> function to verify the hash without
   needing separate storage for the salt or algorithm information.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.password-hash-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.4.0</td>
       <td>
        The default value of the <code class="literal">cost</code> option of the
        <strong><code><a href="password.constants.php#constant.password-bcrypt">PASSWORD_BCRYPT</a></code></strong> algorithm was increased from
        <code class="literal">10</code> to <code class="literal">12</code>.
       </td>
      </tr>

      <tr>
       <td>8.3.0</td>
       <td>
        <span class="function"><strong>password_hash()</strong></span> now sets the underlying
        <span class="exceptionname"><a href="class.random-randomexception.php" class="exceptionname">Random\RandomException</a></span> as the
        <span class="property"><a href="class.exception.php#exception.props.previous">Exception::$previous</a></span> exception when a
        <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> is thrown due to a failure
        in the salt generation.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        <span class="function"><strong>password_hash()</strong></span> no longer returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure, instead a
        <span class="classname"><a href="class.valueerror.php" class="classname">ValueError</a></span> will be thrown if the password hashing algorithm
        is not valid, or an <span class="classname"><a href="class.error.php" class="classname">Error</a></span> if the password hashing failed
        for an unknown error.
       </td>
      </tr>

      <tr>
       <td>8.0.0</td>
       <td>
        The <code class="parameter">algo</code> parameter is nullable now.
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        The <code class="parameter">algo</code> parameter expects a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> now, but still accepts
        <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>s for backward compatibility.
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        The sodium extension provides an alternative implementation for Argon2 passwords.
       </td>
      </tr>

      <tr>
       <td>7.3.0</td>
       <td>
        Support for Argon2id passwords using <strong><code><a href="password.constants.php#constant.password-argon2id">PASSWORD_ARGON2ID</a></code></strong> was added.
       </td>
      </tr>

      <tr>
       <td>7.2.0</td>
       <td>
        Support for Argon2i passwords using <strong><code><a href="password.constants.php#constant.password-argon2i">PASSWORD_ARGON2I</a></code></strong> was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.password-hash-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 <span class="function"><strong>password_hash()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">password_hash</span><span style="color: #007700">(</span><span style="color: #DD0000">"rasmuslerdorf"</span><span style="color: #007700">, </span><span style="color: #0000BB">PASSWORD_DEFAULT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад виведе щось
схоже на:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.</pre>
</div>
    </div>
   </div>
  </p> 
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Приклад #2 <span class="function"><strong>password_hash()</strong></span> example setting cost manually</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$options </span><span style="color: #007700">= [<br />    </span><span style="color: #FF8000">// Increase the bcrypt cost from 12 to 13.<br />    </span><span style="color: #DD0000">'cost' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">13</span><span style="color: #007700">,<br />];<br />echo </span><span style="color: #0000BB">password_hash</span><span style="color: #007700">(</span><span style="color: #DD0000">"rasmuslerdorf"</span><span style="color: #007700">, </span><span style="color: #0000BB">PASSWORD_BCRYPT</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад виведе щось
схоже на:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">$2y$13$xeDfQumlmdm0Sco.4qmH1OGfUUmOcuRmfae0dPJhjX1Bq0yYhqbNi</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Приклад #3 <span class="function"><strong>password_hash()</strong></span> example finding a good cost</strong></p>
    <div class="example-contents"><p>
     This code will benchmark the machine to determine how high of a cost can be used
     without deteriorating user experience. It is recommended to set the highest cost
     that does not slow down other operations the machine needs to perform. 11 is a
     good baseline, and more is better if the machine is fast enough. The code below
     aims for ≤ 350 milliseconds stretching time, which is an appropriate delay for
     systems handling interactive logins.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$timeTarget </span><span style="color: #007700">= </span><span style="color: #0000BB">0.350</span><span style="color: #007700">; </span><span style="color: #FF8000">// 350 milliseconds<br /><br /></span><span style="color: #0000BB">$cost </span><span style="color: #007700">= </span><span style="color: #0000BB">11</span><span style="color: #007700">;<br />do {<br />    </span><span style="color: #0000BB">$cost</span><span style="color: #007700">++;<br />    </span><span style="color: #0000BB">$start </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">password_hash</span><span style="color: #007700">(</span><span style="color: #DD0000">"test"</span><span style="color: #007700">, </span><span style="color: #0000BB">PASSWORD_BCRYPT</span><span style="color: #007700">, [</span><span style="color: #DD0000">"cost" </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$cost</span><span style="color: #007700">]);<br />    </span><span style="color: #0000BB">$end </span><span style="color: #007700">= </span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br />} while ((</span><span style="color: #0000BB">$end </span><span style="color: #007700">- </span><span style="color: #0000BB">$start</span><span style="color: #007700">) &lt; </span><span style="color: #0000BB">$timeTarget</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"Appropriate Cost Found: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$cost </span><span style="color: #007700">- </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад виведе щось
схоже на:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Appropriate Cost Found: 13</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4">
    <p><strong>Приклад #4 <span class="function"><strong>password_hash()</strong></span> example using Argon2i</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Argon2i hash: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">password_hash</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">, </span><span style="color: #0000BB">PASSWORD_ARGON2I</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад виведе щось
схоже на:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Argon2i hash: $argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.password-hash-notes">
  <h3 class="title">Примітки</h3>
  <div class="caution"><strong class="caution">Застереження</strong>
   <p class="para">
    It is strongly recommended not to provide an explicit salt for this function.
    A secure salt will automatically be created if no salt is specified.
   </p>
   <p class="para">
    As noted above, providing the <code class="literal">salt</code> option in PHP 7.0.0
    will generate a deprecation warning. Support for providing a salt explicitly
    has been removed in PHP 8.0.0.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <p class="para">
    It is recommended to test this function on the machine used, adjusting the cost parameter(s)
    so that execution of the function takes less than 350 milliseconds for interactive logins.
    The script in the above example will help choosing an appropriate bcrypt cost for the given
    machine.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <span class="simpara">
    Updates to supported algorithms by this function (or changes to the default one) must follow 
    the following rules:
   </span>
   <p class="para">
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
      Any new algorithm must be in core for at least 1 full release of PHP
      prior to becoming default. So if, for example, a new algorithm is added
      in 7.5.5, it would not be eligible for default until 7.7 (since 7.6
      would be the first full release). But if a different algorithm was added
      in 7.6.0, it would also be eligible for default at 7.7.0.
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       The default should only change in a full release (7.3.0, 8.0.0, etc)
       and not in a revision release.  The only exception to this is in an
       emergency when a critical security flaw is found in the current
       default.
      </span>
     </li>
    </ul>
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.password-hash-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.password-verify.php" class="function" rel="rdfs-seeAlso">password_verify()</a> - Verifies that a password matches a hash</span></li>
    <li><span class="function"><a href="function.password-needs-rehash.php" class="function" rel="rdfs-seeAlso">password_needs_rehash()</a> - Checks if the given hash matches the given options</span></li>
    <li><span class="function"><a href="function.crypt.php" class="function" rel="rdfs-seeAlso">crypt()</a> - One-way string hashing</span></li>
    <li><span class="function"><a href="function.sodium-crypto-pwhash-str.php" class="function" rel="rdfs-seeAlso">sodium_crypto_pwhash_str()</a> - Get an ASCII-encoded hash</span></li>
   </ul>
  </p>
 </div>


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