<!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)</p><p class="refpurpose"><span class="refname">sha1</span> — <span class="dc-title">Calculate the sha1 hash of a string</span></p> </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"> = false</span></span> ] )</div> <p class="simpara"> Calculates the sha1 hash of <em><code class="parameter">str</code></em> 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> <span class="term"><em><code class="parameter">str</code></em></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">raw_output</code></em></span> <dd> <p class="para"> If the optional <em><code class="parameter">raw_output</code></em> 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> </dt> </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 changelog" id="refsect1-function.sha1-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.0.0</td> <td> The <em><code class="parameter">raw_output</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.sha1-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4861"> <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 notes" id="refsect1-function.sha1-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <strong>Secure password hashing</strong><br /> <p class="para"> It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm. See <a href="faq.passwords.html#faq.passwords.fasthash" class="link">here</a> for details. </p> </p></blockquote> </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> </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>