⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.crypt.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>One-way string encryption (hashing)</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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 encryption (hashing)</span></p> </div>  <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">string</span> <span class="methodname"><b><b>crypt</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$str</tt></span>   [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$salt</tt></span>  ] )</div>  <p class="para rdfs-comment">   <b>crypt()</b> will return an encrypted string using the   standard Unix <abbr class="abbrev">DES</abbr>-based encryption algorithm or   alternative algorithms that may be available on the system.  </p>  <p class="para">   Some operating systems support more than one type of encryption.  In   fact, sometimes the standard DES-based encryption is replaced by an   MD5-based encryption algorithm.  The encryption type is triggered by the   salt argument.  At install time, PHP determines the capabilities of the   crypt function and will accept salts for other encryption types.  If no   salt is provided, PHP will auto-generate a standard two character salt by   default, unless the default encryption type on the system is MD5, in   which case a random MD5-compatible salt is generated.  PHP sets a   constant named <b><tt>CRYPT_SALT_LENGTH</tt></b> which tells you   whether a regular two character salt applies to your system or the longer   twelve character salt is applicable.  </p>  <p class="para">   The standard DES-based encryption <b>crypt()</b> returns the   salt as the first two characters of the output. It also only uses the   first eight characters of <i><tt class="parameter">str</tt></i>, 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   encryption 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">     <b><tt>CRYPT_STD_DES</tt></b> - Standard DES-based encryption with a two character salt    </span>   </li>   <li class="listitem">    <span class="simpara">     <b><tt>CRYPT_EXT_DES</tt></b> - Extended DES-based encryption with a nine character salt    </span>   </li>   <li class="listitem">    <span class="simpara">     <b><tt>CRYPT_MD5</tt></b> - MD5 encryption with a twelve character salt starting with     $1$    </span>   </li>   <li class="listitem">    <span class="simpara">     <b><tt>CRYPT_BLOWFISH</tt></b> - Blowfish encryption with a sixteen character salt     starting with $2$ or $2a$    </span>   </li>  </ul> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">str</tt></i></span>     <dd>      <p class="para">       The string to be encrypted.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">salt</tt></i></span>     <dd>      <p class="para">       An optional salt string to base the encryption on. If not provided,       one will be randomly generated by PHP each time you call this function.      </p>      <p class="para">       If you are using the supplied salt, you should be aware that the salt       is generated once. If you are calling this function repeatedly, this       may impact both appearance and security.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   Returns the encrypted string.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>crypt()</b> examples</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$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">$password</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">$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>   <div class="example">    <p><b>Example #2 Using <b>crypt()</b> with htpasswd</b></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>   <div class="example">    <p><b>Example #3 Using <b>crypt()</b> with different encryption types</b></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">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$rasmuslerd...........$'</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"><pre><div class="cdata"><pre>Standard DES: rl.3StKT.4T8MExtended DES: _J9..rasmBYk8r9AiWNcMD5:          $1$rasmusle$rISCgZzpwk3UhDidwXvin0Blowfish:     $2a$07$rasmuslerd............nIdrcHdxcUxWomQX9j6kvERCFjTg7Ra</pre></div>    </pre></div>   </div>  </p> </div> <div class="refsect1 notes">  <h3 class="title">Notes</h3>  <blockquote><p><b class="note">Note</b>:    <span class="simpara">    There is no decrypt function, since <b>crypt()</b> uses a    one-way algorithm.   </span>  </p></blockquote> </div> <div class="refsect1 seealso">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.md5.html" class="function" rel="rdfs-seeAlso">md5()</a></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 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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -