<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.crypt.php',
    1 => 'crypt',
    2 => 'One-way string hashing',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Функції',
  ),
  'prev' => 
  array (
    0 => 'function.crc32.php',
    1 => 'crc32',
  ),
  'next' => 
  array (
    0 => 'function.echo.php',
    1 => 'echo',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/crypt.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.crypt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">crypt</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">crypt</span> &mdash; <span class="dc-title">One-way string hashing</span></p>

 </div>
 
 <div id="function.crypt-refsynopsisdiv">
  <div class="warning"><strong class="warning">Увага</strong><p class="simpara"> Ця функція (поки-що)
не є безпечною для двійкових даних!</p></div>
 </div>
 
 <div class="refsect1 description" id="refsect1-function.crypt-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>crypt</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">$string</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$salt</code></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>crypt()</strong></span> will return a hashed string using the
   standard Unix <abbr class="abbrev">DES</abbr>-based algorithm or
   alternative algorithms. <span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span> is
   compatible with <span class="function"><strong>crypt()</strong></span>. Therefore, password
   hashes created by <span class="function"><strong>crypt()</strong></span> can be used with
   <span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span>.
  </p>
  <p class="para">
   Prior to PHP 8.0.0, the <code class="parameter">salt</code> parameter was optional. However, <span class="function"><strong>crypt()</strong></span> creates a weak hash without the <code class="parameter">salt</code>, and raises an <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> error without it. Make sure to specify a strong enough salt for better security. 
  </p>
  <p class="para">
   <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> uses a strong hash, generates a strong salt, and applies proper rounds automatically. <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> is a simple <span class="function"><strong>crypt()</strong></span> wrapper and compatible with existing password hashes. Use of <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> is encouraged.
  </p>
  <p class="para">
   The hash type is triggered by the salt argument.
   If no salt is provided, PHP will
   auto-generate either a standard two character (DES) salt, or a twelve
   character (MD5), depending on the availability of MD5 crypt().  PHP sets a
   constant named <strong><code><a href="string.constants.php#constant.crypt-salt-length">CRYPT_SALT_LENGTH</a></code></strong> which indicates the
   longest valid salt allowed by the available hashes.
  </p>
  <p class="para">
   The standard DES-based <span class="function"><strong>crypt()</strong></span> returns the
   salt as the first two characters of the output. It also only uses the
   first eight characters of <code class="parameter">string</code>, so longer strings
   that start with the same eight characters will generate the same result
   (when the same salt is used).
  </p>
  <p class="simpara">
   The following hash types are supported:
  </p>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-std-des">CRYPT_STD_DES</a></code></strong> - Standard DES-based hash with a two character salt
       from the alphabet &quot;./0-9A-Za-z&quot;. Using invalid characters in the salt will cause
       crypt() to fail.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-ext-des">CRYPT_EXT_DES</a></code></strong> - Extended DES-based hash. The &quot;salt&quot; is a
     9-character string consisting of an underscore followed by 4 characters of iteration count
     and 4 characters of salt. Each of these 4-character strings encode 24 bits, least significant
     character first. The values <code class="literal">0</code> to <code class="literal">63</code> are encoded as
     <code class="literal">./0-9A-Za-z</code>. Using invalid characters in the salt will cause crypt() to fail.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-md5">CRYPT_MD5</a></code></strong> - MD5 hashing with a twelve character salt starting with
     $1$
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-blowfish">CRYPT_BLOWFISH</a></code></strong> - Blowfish hashing with a salt as
     follows: &quot;$2a$&quot;, &quot;$2x$&quot; or &quot;$2y$&quot;, a two digit cost parameter, &quot;$&quot;, and
     22 characters from the alphabet &quot;./0-9A-Za-z&quot;. Using characters outside of
     this range in the salt will cause crypt() to return a zero-length string.
     The two digit cost parameter is the base-2 logarithm of the iteration
     count for the underlying Blowfish-based hashing algorithm and must be
     in range 04-31, values outside this range will cause crypt() to fail.
     &quot;$2x$&quot; hashes are potentially weak; &quot;$2a$&quot; hashes are compatible and
     mitigate this weakness. For new hashes, &quot;$2y$&quot; should be used.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-sha256">CRYPT_SHA256</a></code></strong> - SHA-256 hash with a sixteen character salt
     prefixed with $5$. If the salt string starts with &#039;rounds=&lt;N&gt;$&#039;, the numeric value of N
     is used to indicate how many times the hashing loop should be executed, much like the cost
     parameter on Blowfish. The default number of rounds is 5000, there is a minimum of
     1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to
     the nearest limit.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-sha512">CRYPT_SHA512</a></code></strong> - SHA-512 hash with a sixteen character salt
     prefixed with $6$. If the salt string starts with &#039;rounds=&lt;N&gt;$&#039;, the numeric value of N
     is used to indicate how many times the hashing loop should be executed, much like the cost
     parameter on Blowfish. The default number of rounds is 5000, there is a minimum of
     1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to
     the nearest limit.
    </span>
   </li>
  </ul>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.crypt-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       The string to be hashed.
      </p>
      <div class="caution"><strong class="caution">Застереження</strong>
       <p class="para">
        Using the <strong><code><a href="string.constants.php#constant.crypt-blowfish">CRYPT_BLOWFISH</a></code></strong> algorithm, will result
        in the <code class="parameter">string</code> parameter being truncated to a
        maximum length of 72 bytes.
       </p>
      </div>
     </dd>
    
    
     <dt><code class="parameter">salt</code></dt>
     <dd>
      <p class="para">
       A salt string to base the hashing on. If not provided, the
       behaviour is defined by the algorithm implementation and can lead to
       unexpected results.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.crypt-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Returns the hashed string or a string that is shorter than 13 characters
   and is guaranteed to differ from the salt on failure.
  </p>
  <div class="warning"><strong class="warning">Увага</strong>
   <p class="simpara">
    When validating passwords, a string comparison function that isn&#039;t
    vulnerable to timing attacks should be used to compare the output of
    <span class="function"><strong>crypt()</strong></span> to the previously known hash. PHP
    provides <span class="function"><a href="function.hash-equals.php" class="function">hash_equals()</a></span> for this purpose.
   </p>
  </div>
 </div>

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

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       The <code class="parameter">salt</code> is no longer optional.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.crypt-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 <span class="function"><strong>crypt()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$user_input </span><span style="color: #007700">= </span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$hashed_password </span><span style="color: #007700">= </span><span style="color: #DD0000">'$6$rounds=1000000$NJy4rIPjpOaU$0ACEYGg/aKCY3v8O8AfyiO7CTfZQ8/W231Qfh2tRLmfdvFD6XfHk12u6hMr9cYIA4hnpjLNSTRtUwYr9km9Ij/'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Validate an existing crypt() hash in a way that is compatible with non-PHP software.<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">hash_equals</span><span style="color: #007700">(</span><span style="color: #0000BB">$hashed_password</span><span style="color: #007700">, </span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #0000BB">$user_input</span><span style="color: #007700">, </span><span style="color: #0000BB">$hashed_password</span><span style="color: #007700">))) {<br />   echo </span><span style="color: #DD0000">"Password verified!"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

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

 
 <div class="refsect1 notes" id="refsect1-function.crypt-notes">
  <h3 class="title">Примітки</h3>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
   <span class="simpara">
    There is no decrypt function, since <span class="function"><strong>crypt()</strong></span> uses a
    one-way algorithm.
   </span>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.crypt-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.hash-equals.php" class="function" rel="rdfs-seeAlso">hash_equals()</a> - Timing attack safe string comparison</span></li>
    <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>The Unix man page for your crypt function for more information</li>
   </ul>
  </p>
 </div>

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