Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 2328

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>One-way string hashing</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.crc32.html">crc32</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.echo.html">echo</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.crypt" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">crypt</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">crypt</span> &mdash; <span class="dc-title">One-way string hashing</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.crypt-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>crypt</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$salt</code></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 that may be available on the system.
  </p>
  <p class="para">
   The <em><code class="parameter">salt</code></em> parameter is optional. However,  <span class="function"><strong>crypt()</strong></span> creates weak password without <em><code class="parameter">salt</code></em>. PHP 5.6 or later raise E_NOTICE error without it. Make sure specify strong enough salt for better security. 
  </p>
  <p class="para">
    <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span> uses strong hash, generates strong salt, applies proper rounds automatically.  <span class="function"><a href="function.password-hash.html" class="function">password_hash()</a></span> is 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.html" class="function">password_hash()</a></span> is encouraged.
  </p>
  <p class="para">
   Some operating systems support more than one type of hash.  In
   fact, sometimes the standard DES-based algorithm is replaced by an
   MD5-based algorithm.  The hash type is triggered by the salt argument.
   Prior to 5.3, PHP would determine the available algorithms at install-time
   based on the system&#039;s crypt(). 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>CRYPT_SALT_LENGTH</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 <em><code class="parameter">str</code></em>, 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">
   On systems where the crypt() function supports multiple
   hash types, the following constants are set to 0 or 1
   depending on whether the given type is available:
  </p>
  <ul class="itemizedlist">
   <li class="listitem">
    <span class="simpara">
     <strong><code>CRYPT_STD_DES</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>CRYPT_EXT_DES</code></strong> - Extended DES-based hash. The &quot;salt&quot; is a
     9-character string consisting of an underscore followed by 4 bytes of iteration count and
     4 bytes of salt. These are encoded as printable characters, 6 bits per character, least
     significant character first. The values 0 to 63 are encoded as &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>CRYPT_MD5</code></strong> - MD5 hashing with a twelve character salt starting with
     $1$
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code>CRYPT_BLOWFISH</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 algorithmeter and must be
     in range 04-31, values outside this range will cause crypt() to fail.
     Versions of PHP before 5.3.7 only support &quot;$2a$&quot; as the salt prefix: PHP
     5.3.7 introduced the new prefixes to fix a security weakness in the
     Blowfish implementation.  Please refer to
     <a href="http://www.php.net/security/crypt_blowfish.php" class="link external">&raquo;&nbsp;this document</a> for full
     details of the security fix, but to summarise, developers targeting only
     PHP 5.3.7 and later should use &quot;$2y$&quot; in preference to &quot;$2a$&quot;.
    </span>
   </li>
   <li class="listitem">
    <span class="simpara">
     <strong><code>CRYPT_SHA256</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>CRYPT_SHA512</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>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    As of PHP 5.3.0, PHP contains its own implementation and will use that
    if the system lacks of support for one or more of the algorithms.
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.crypt-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">str</code></em></span>
     <dd>

      <p class="para">
       The string to be hashed.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">salt</code></em></span>
     <dd>

      <p class="para">
       An optional 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>

    </dt>

   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.crypt-returnvalues">
  <h3 class="title">Return Values</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>

 
 <div class="refsect1 changelog" id="refsect1-function.crypt-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.6.0</td>
       <td>
        Raise E_NOTICE security warning if <em><code class="parameter">salt</code></em> is omitted.
       </td>
      </tr>

      <tr>
       <td>5.3.7</td>
       <td>
        Added <em>$2x$</em> and <em>$2y$</em> Blowfish
        modes to deal with potential high-bit attacks.
       </td>
      </tr>

      <tr>
       <td>5.3.2</td>
       <td>
        Added SHA-256 and SHA-512 crypt based on Ulrich Drepper&#039;s 
        <a href="http://people.redhat.com/drepper/SHA-crypt.txt" class="link external">&raquo;&nbsp;implementation</a>.
       </td>
      </tr>

      <tr>
       <td>5.3.2</td>
       <td>
        Fixed Blowfish behaviour on invalid rounds to return &quot;failure&quot; 
        string (&quot;*0&quot; or &quot;*1&quot;), instead of falling back to DES.
       </td>
      </tr>

      <tr>
       <td>5.3.0</td>
       <td>
        PHP now contains its own implementation for the MD5 crypt, Standard DES,
        Extended DES and the Blowfish algorithms and will use that if the system
        lacks of support for one or more of the algorithms.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.crypt-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4824">
    <p><strong>Example #1  <span class="function"><strong>crypt()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$hashed_password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #DD0000">'mypassword'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;let&nbsp;the&nbsp;salt&nbsp;be&nbsp;automatically&nbsp;generated<br /><br />/*&nbsp;You&nbsp;should&nbsp;pass&nbsp;the&nbsp;entire&nbsp;results&nbsp;of&nbsp;crypt()&nbsp;as&nbsp;the&nbsp;salt&nbsp;for&nbsp;comparing&nbsp;a<br />&nbsp;&nbsp;&nbsp;password,&nbsp;to&nbsp;avoid&nbsp;problems&nbsp;when&nbsp;different&nbsp;hashing&nbsp;algorithms&nbsp;are&nbsp;used.&nbsp;(As<br />&nbsp;&nbsp;&nbsp;it&nbsp;says&nbsp;above,&nbsp;standard&nbsp;DES-based&nbsp;password&nbsp;hashing&nbsp;uses&nbsp;a&nbsp;2-character&nbsp;salt,<br />&nbsp;&nbsp;&nbsp;but&nbsp;MD5-based&nbsp;hashing&nbsp;uses&nbsp;12.)&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #0000BB">$user_input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$hashed_password</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">$hashed_password</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Password&nbsp;verified!"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4825">
    <p><strong>Example #2 Using  <span class="function"><strong>crypt()</strong></span> with htpasswd</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: #FF8000">//&nbsp;Set&nbsp;the&nbsp;password<br /></span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'mypassword'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;the&nbsp;hash,&nbsp;letting&nbsp;the&nbsp;salt&nbsp;be&nbsp;automatically&nbsp;generated<br /></span><span style="color: #0000BB">$hash&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4826">
    <p><strong>Example #3 Using  <span class="function"><strong>crypt()</strong></span> with different hash types</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: #FF8000">/*&nbsp;These&nbsp;salts&nbsp;are&nbsp;examples&nbsp;only,&nbsp;and&nbsp;should&nbsp;not&nbsp;be&nbsp;used&nbsp;verbatim&nbsp;in&nbsp;your&nbsp;code.<br />&nbsp;&nbsp;&nbsp;You&nbsp;should&nbsp;generate&nbsp;a&nbsp;distinct,&nbsp;correctly-formatted&nbsp;salt&nbsp;for&nbsp;each&nbsp;password.<br />*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">CRYPT_STD_DES&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Standard&nbsp;DES:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rl'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">CRYPT_EXT_DES&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Extended&nbsp;DES:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'_J9..rasm'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">CRYPT_MD5&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'MD5:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$1$rasmusle$'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">CRYPT_BLOWFISH&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Blowfish:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$2a$07$usesomesillystringforsalt$'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">CRYPT_SHA256&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'SHA-256:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$5$rounds=5000$usesomesillystringforsalt$'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">CRYPT_SHA512&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'SHA-512:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">crypt</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$6$rounds=5000$usesomesillystringforsalt$'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Standard DES: rl.3StKT.4T8M
Extended DES: _J9..rasmBYk8r9AiWNc
MD5:          $1$rasmusle$rISCgZzpwk3UhDidwXvin0
Blowfish:     $2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi
SHA-256:      $5$rounds=5000$usesomesillystri$KqJWpanXZHKq2BOB43TSaYhEWsQ1Lr5QNyPCDH/Tp.6
SHA-512:      $6$rounds=5000$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21
</pre></div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.crypt-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">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">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.password-hash.html" class="function" rel="rdfs-seeAlso">password_hash()</a> - Creates a password hash</span></li>
    <li class="member"> <span class="function"><a href="function.md5.html" class="function" rel="rdfs-seeAlso">md5()</a> - Calculate the md5 hash of a string</span></li>
    <li class="member">The <a href="ref.mcrypt.html" class="link">Mcrypt</a> extension</li>
    <li class="member">The Unix man page for your crypt function for more information</li>
   </ul>
  </p>
 </div>

 
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.crc32.html">crc32</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.echo.html">echo</a></div>
 <div class="up"><a href="ref.strings.html">String Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>