<!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>Calculate the sha1 hash of a string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sha1-file.html">sha1_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.similar-text.html">similar_text</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.sha1" class="refentry"> <div class="refnamediv"> <h1 class="refname">sha1</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">sha1</span> — <span class="dc-title">Calculate the sha1 hash of a string</span></p> </div> <div id="function.sha1-refsynopsisdiv"> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm. See the <a href="faq.passwords.html#faq.passwords.fasthash" class="link">Password Hashing FAQ</a> for details and best practices. </p> </div> </div> <div class="refsect1 description" id="refsect1-function.sha1-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>sha1</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$raw_output</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span> ] )</div> <p class="simpara"> Calculates the sha1 hash of <code class="parameter">str</code> using the <a href="http://www.faqs.org/rfcs/rfc3174" class="link external">» US Secure Hash Algorithm 1</a>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.sha1-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">str</code></dt> <dd> <p class="para"> The input string. </p> </dd> <dt> <code class="parameter">raw_output</code></dt> <dd> <p class="para"> If the optional <code class="parameter">raw_output</code> is set to <strong><code>TRUE</code></strong>, then the sha1 digest is instead returned in raw binary format with a length of 20, otherwise the returned value is a 40-character hexadecimal number. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.sha1-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the sha1 hash as a string. </p> </div> <div class="refsect1 examples" id="refsect1-function.sha1-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="metaphone.example.phonemes"> <p><strong>Example #1 A <span class="function"><strong>sha1()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">;<br /><br />if (</span><span style="color: #0000BB">sha1</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">) === </span><span style="color: #DD0000">'d0be2dc421be4fcd0172e5afceea3970e2f3d940'</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Would you like a green or red apple?"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.sha1-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.sha1-file.html" class="function" rel="rdfs-seeAlso">sha1_file()</a> - Calculate the sha1 hash of a file</span></li> <li class="member"><span class="function"><a href="function.crc32.html" class="function" rel="rdfs-seeAlso">crc32()</a> - Calculates the crc32 polynomial of a string</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"><span class="function"><a href="function.hash.html" class="function" rel="rdfs-seeAlso">hash()</a> - Generate a hash value (message digest)</span></li> <li class="member"><span class="function"><a href="function.crypt.html" class="function" rel="rdfs-seeAlso">crypt()</a> - One-way string hashing</span></li> <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> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sha1-file.html">sha1_file</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.similar-text.html">similar_text</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>