<?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 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.crypt.php',
    1 => 'crypt',
    2 => '文字列の一方向のハッシュ化を行う',
  ),
  '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' => 'ja',
    '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>
   パラメータは必須ではありませんでした。ただ、これを省略すると
   <span class="function"><strong>crypt()</strong></span>
   が作るハッシュが弱いものになってしまっていました。
   このパラメータを省略した場合には、E_NOTICE が発生していました。
   十分に強い salt を指定して、セキュリティを確保しましょう。
  </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">
   ハッシュ方式は、salt 引数によって決まります。
   salt を省略した場合は、標準の 2 文字 (DES) の salt を自動生成します。
   あるいは、MD5 crypt() が使えれば 12 文字 (MD5) の salt を自動生成します。
   PHP の定数 <strong><code><a href="string.constants.php#constant.crypt-salt-length">CRYPT_SALT_LENGTH</a></code></strong> には、
   ハッシュで使用できる salt の最大長が格納されています。
  </p>
  <p class="para">
   標準の DES ベースの場合、<span class="function"><strong>crypt()</strong></span>
   は出力の最初の 2 文字を salt として使用します。また、
   <code class="parameter">string</code> の最初の 8 文字しか使用しません。
   つまり、最初の 8 文字が同じである長い文字列は、
   同じ salt を使う限り同じ結果となります。
  </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; からなる 2 文字の salt を使用するもの。
       salt として無効な文字を使うと 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 ベースのハッシュ。
     &quot;salt&quot; は 9 文字で、
     アンダースコアの後に 4 文字の反復回数と 4 文字の salt が続きます。
     これらの4文字それぞれが、24ビットにエンコードされ、
     下位の文字から順に並びます。
     <code class="literal">0</code> から <code class="literal">63</code> までの値は
     <code class="literal">./0-9A-Za-z</code> の範囲内の文字でエンコードされます。
     salt に無効な文字を使うと crypt() は失敗します。
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-md5">CRYPT_MD5</a></code></strong> - $1$ ではじまる 12 文字の salt を使用する MD5 ハッシュ。
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code><a href="string.constants.php#constant.crypt-blowfish">CRYPT_BLOWFISH</a></code></strong> - Blowfish ハッシュ。salt の形式は、
     &quot;$2a$&quot; か &quot;$2x$&quot; あるいは &quot;$2y$&quot;、2 桁のコストパラメータ、&quot;$&quot;、そして文字 &quot;./0-9A-Za-z&quot;
     からなる 22 文字となります。
     この範囲外の文字を salt に使うと、crypt() は長さゼロの文字列を返します。
     2 桁のコストパラメータは反復回数の 2 を底とする対数で、
     これは Blowfish ベースのハッシュアルゴリズムで使います。
     この値は 04 から 31 までの範囲でなければならず、
     それ以外の値の場合は crypt() は失敗します。
     &quot;$2x$&quot; ハッシュは潜在的に弱いハッシュです。
     &quot;$2a$&quot; ハッシュはこれと互換性があり、この弱点を軽減します。
     新しくハッシュを生成する場合は、&quot;$2y$&quot; を使うべきです。
    </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 文字の salt を組み合わせたもの。salt 文字列が
     &#039;rounds=&lt;N&gt;$&#039; で始まる場合は、数値 N がハッシュループの実行回数を表します。
     これは Blowfish のコストパラメータのようなものです。
     rounds のデフォルトは 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 文字の salt を組み合わせたもの。salt 文字列が
     &#039;rounds=&lt;N&gt;$&#039; で始まる場合は、数値 N がハッシュループの実行回数を表します。
     これは Blowfish のコストパラメータのようなものです。
     rounds のデフォルトは 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">
       ハッシュのもととなる salt 文字列。省略した場合の挙動は
       アルゴリズムの実装によって決まるので、予期せぬ結果となることがあり得ます。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.crypt-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   ハッシュした文字列を返します。
   失敗した場合は、salt とは異なることが保証されている 13 文字未満の文字列を返します。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="simpara">
    パスワードを検証するときの文字列比較関数は、
    タイミング攻撃に対して脆弱ではないものでなければいけません。
    これをもちいて、<span class="function"><strong>crypt()</strong></span> の出力と既知のハッシュとを比較します。
    この比較を行うために、<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">// PHP 以外のソフトウェアと互換性がある方法で、既存の crypt() ハッシュを検証します<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> が使用しているのは単方向アルゴリズムだからです。
   </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> - パスワードハッシュを作る</span></li>
    <li>暗号化関数についての Unix man ページ</li>
   </ul>
  </p>
 </div>


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