<?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 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.crypt.php',
    1 => 'crypt',
    2 => '单向字符串散列',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => '字符串 函数',
  ),
  'prev' => 
  array (
    0 => 'function.crc32.php',
    1 => 'crc32',
  ),
  'next' => 
  array (
    0 => 'function.echo.php',
    1 => 'echo',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    '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">单向字符串散列</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> 返回基于标准 UNIX <abbr class="abbrev">DES</abbr>
   算法或替代算法的散列字符串。<span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span>
   兼容 <span class="function"><strong>crypt()</strong></span>。因此，由 <span class="function"><strong>crypt()</strong></span>
   创建的密码散列可以跟 <span class="function"><a href="function.password-verify.php" class="function">password_verify()</a></span> 一起使用。
  </p>
  <p class="para">
   在 PHP 8.0.0 之前，<code class="parameter">salt</code> 参数是可选的。然而，如果没有
   <code class="parameter">salt</code>，<span class="function"><strong>crypt()</strong></span> 
   会创建弱散列。在没有它的情况下引发 <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>
   级别的错误。为了更好的安全性，请确保指定足够强度的盐值。
  </p>
  <p class="para">
   <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> 使用了强散列，产生足够强的盐值，并自动应用合适的轮次。<span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span> 
   是<span class="function"><strong>crypt()</strong></span> 的简单封装，并且与现有的密码散列兼容。推荐使用 <span class="function"><a href="function.password-hash.php" class="function">password_hash()</a></span>。
  </p>
  <p class="para">
   散列类型由盐值参数触发。如果没有提供盐值，PHP 将自动生成一个 2 个字符（DES）或者 12 个字符（MD5）的盐值 ，这取决于
   MD5 crypt() 的可用性。PHP 设置了名为 <strong><code><a href="string.constants.php#constant.crypt-salt-length">CRYPT_SALT_LENGTH</a></code></strong> 的常量，用来表示可用散列允许的最长有效盐值。
  </p>
  <p class="para">
   基于标准 DES 算法的 <span class="function"><strong>crypt()</strong></span> 在输出内容的开始位置返回两个字符的盐值。它也只使用
   <code class="parameter">string</code> 的开始 8 个字符，所以更长的以相同 8 个字符开始的字符串也将生成相同的结果（当使用了相同的盐值时）。
  </p>
  <p class="simpara">
   支持以下散列类型：
  </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> - 基于标准 DES 算法的散列使用 &quot;./0-9A-Za-z&quot; 字符中的两个字符作为盐值。在盐值中使用非法的字符将导致 crypt() 失败。
    </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> - 扩展的基于 DES 算法的散列。“sale” 为 9 个字符的字符串，由 1 个下划线后面跟着 4 字符循环次数和 4 字符盐值组成。这些 4 字符字符串都编码为 24 字节，最低有效位在前。值 <code class="literal">0</code> 到 <code class="literal">63</code> 被编码为 <code class="literal">./0-9A-Za-z</code>。在盐值中使用非法的字符将导致 crypt() 失败。
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-md5">CRYPT_MD5</a></code></strong> - MD5 散列使用一个以 $1$ 开始的 12 字符的字符串盐值。
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-blowfish">CRYPT_BLOWFISH</a></code></strong> - Blowfish 散列使用如下盐值：“$2a$”、“$2x$” 或 “$2y$”，两位 cost 参数，“$” 以及 22 位由 “./0-9A-Za-z” 中的字符组合而成的字符串。在盐值中使用此范围之外的字符将导致 crypt() 返回一个空字符串。两位 cost 参数是循环次数以 2 为底的对数，它的范围是 04-31，超出这个范围将导致 crypt() 失败。
     “$2x$” 可能很弱，“$2x$” 散列对其兼容并增强安全性。对于新的散列，应该使用“$2y$”。
    </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 算法使用一个以 $5$ 开头的 16 字符字符串盐值进行散列。如果盐值字符串以 “rounds=&lt;N&gt;$” 开头，N 的数字值将被用来指定散列循环的执行次数，这点很像 Blowfish 算法的 cost 参数。默认的循环次数是 5000，最小是 1000，最大是 999,999,999。超出这个范围的 N 将会被转换为最接近的值。
    </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 算法使用一个以 $6$ 开头的 16 字符字符串盐值进行散列。如果盐值字符串以 “rounds=&lt;N&gt;$” 开头，N 的数字值将被用来指定散列循环的执行次数，这点很像 Blowfish 算法的 cost 参数。默认的循环次数是 5000，最小是 1000，最大是 999,999,999。超出这个范围的 N 将会被转换为最接近的值。
    </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">
       待散列的字符串。
      </p>
      <div class="caution"><strong class="caution">警告</strong>
       <p class="para">
        使用 <strong><code><a href="string.constants.php#constant.crypt-blowfish">CRYPT_BLOWFISH</a></code></strong> 算法将导致 <code class="parameter">string</code> 被裁剪为一个最长72 字节的字符串。
       </p>
      </div>
     </dd>
    
    
     <dt><code class="parameter">salt</code></dt>
     <dd>
      <p class="para">
       盐值字符串。如果没有提供，算法行为将由不同的算法实现决定，并可能导致不可预料的结束。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.crypt-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回散列后的字符串或一个少于 13 字符的字符串，从而保证在失败时与盐值区分开来。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    当校验密码时，应该使用一个不容易被时间攻击的字符串比较函数来比较<span class="function"><strong>crypt()</strong></span>的输出与之前已知的散列。出于这个目的，PHP 提供了<span class="function"><a href="function.hash-equals.php" class="function">hash_equals()</a></span>。
   </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>
       <code class="parameter">salt</code>不再可选。
      </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> 示例</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">// 验证现有的 crypt() 哈希值，以便与非 PHP 软件兼容。<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">
    由于 <span class="function"><strong>crypt()</strong></span> 使用的是单向算法，因此不存在 decrypt 函数。
   </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> - 可防止时序攻击的字符串比较</span></li>
    <li><span class="function"><a href="function.password-hash.php" class="function" rel="rdfs-seeAlso">password_hash()</a> - 创建密码的散列（hash）</span></li>
    <li>更多关于 crypt 函数的信息，请阅读 Unix man 页面</li>
   </ul>
  </p>
 </div>

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