<!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>Generate a hash value (message digest)</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.hash-update.html">hash_update</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.mcrypt.html">Mcrypt</a></div> <div class="up"><a href="ref.hash.html">Hash Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.hash" class="refentry"> <div class="refnamediv"> <h1 class="refname">hash</h1> <p class="verinfo">(PHP 5 >= 5.1.2, PHP 7, PECL hash >= 1.1)</p><p class="refpurpose"><span class="refname">hash</span> — <span class="dc-title">Generate a hash value (message digest)</span></p> </div> <div class="refsect1 description" id="refsect1-function.hash-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>hash</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$algo</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</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> </div> <div class="refsect1 parameters" id="refsect1-function.hash-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">algo</code></dt> <dd> <p class="para"> Name of selected hashing algorithm (e.g. "md5", "sha256", "haval160,4", etc..) </p> </dd> <dt> <code class="parameter">data</code></dt> <dd> <p class="para"> Message to be hashed. </p> </dd> <dt> <code class="parameter">raw_output</code></dt> <dd> <p class="para"> When set to <strong><code>TRUE</code></strong>, outputs raw binary data. <strong><code>FALSE</code></strong> outputs lowercase hexits. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.hash-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns a string containing the calculated message digest as lowercase hexits unless <code class="parameter">raw_output</code> is set to true in which case the raw binary representation of the message digest is returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.hash-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.4.0</td> <td>The tiger algorithm now uses big-endian byte ordering. See also example below.</td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.hash-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-933"> <p><strong>Example #1 A <span class="function"><strong>hash()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">hash</span><span style="color: #007700">(</span><span style="color: #DD0000">'ripemd160'</span><span style="color: #007700">, </span><span style="color: #DD0000">'The quick brown fox jumped over the lazy dog.'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> ec457d0a974c48d5685a7efa03d137dc8bbde7e3 </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="random-bytes.example.basic"> <p><strong>Example #2 Calculate pre PHP-5.4 tiger hashes with PHP-5.4 and higher</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">old_tiger</span><span style="color: #007700">(</span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #0000BB">$width</span><span style="color: #007700">=</span><span style="color: #0000BB">192</span><span style="color: #007700">, </span><span style="color: #0000BB">$rounds </span><span style="color: #007700">= </span><span style="color: #0000BB">3</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">substr</span><span style="color: #007700">(<br /> </span><span style="color: #0000BB">implode</span><span style="color: #007700">(<br /> </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(<br /> function (</span><span style="color: #0000BB">$h</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">str_pad</span><span style="color: #007700">(</span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">strrev</span><span style="color: #007700">(</span><span style="color: #0000BB">$h</span><span style="color: #007700">)), </span><span style="color: #0000BB">16</span><span style="color: #007700">, </span><span style="color: #DD0000">"0"</span><span style="color: #007700">);<br /> },<br /> </span><span style="color: #0000BB">str_split</span><span style="color: #007700">(</span><span style="color: #0000BB">hash</span><span style="color: #007700">(</span><span style="color: #DD0000">"tiger192,</span><span style="color: #0000BB">$rounds</span><span style="color: #DD0000">"</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">8</span><span style="color: #007700">)<br /> )<br /> ),<br /> </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">48</span><span style="color: #007700">-(</span><span style="color: #0000BB">192</span><span style="color: #007700">-</span><span style="color: #0000BB">$width</span><span style="color: #007700">)/</span><span style="color: #0000BB">4<br /> </span><span style="color: #007700">);<br />}<br />echo </span><span style="color: #0000BB">hash</span><span style="color: #007700">(</span><span style="color: #DD0000">'tiger192,3'</span><span style="color: #007700">, </span><span style="color: #DD0000">'a-string'</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">old_tiger</span><span style="color: #007700">(</span><span style="color: #DD0000">'a-string'</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>Output of the above example in PHP 5.3:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 146a7492719b3564094efe7abbd40a7416fd900179d02773 64359b7192746a14740ad4bb7afe4e097327d0790190fd16 </pre></div> </div> <div class="example-contents"><p>Output of the above example in PHP 5.4:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 64359b7192746a14740ad4bb7afe4e097327d0790190fd16 146a7492719b3564094efe7abbd40a7416fd900179d02773 </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.hash-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.hash-file.html" class="function" rel="rdfs-seeAlso">hash_file()</a> - Generate a hash value using the contents of a given file</span></li> <li class="member"><span class="function"><a href="function.hash-hmac.html" class="function" rel="rdfs-seeAlso">hash_hmac()</a> - Generate a keyed hash value using the HMAC method</span></li> <li class="member"><span class="function"><a href="function.hash-init.html" class="function" rel="rdfs-seeAlso">hash_init()</a> - Initialize an incremental hashing context</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.sha1.html" class="function" rel="rdfs-seeAlso">sha1()</a> - Calculate the sha1 hash of a string</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.hash-update.html">hash_update</a></div> <div class="next" style="text-align: right; float: right;"><a href="book.mcrypt.html">Mcrypt</a></div> <div class="up"><a href="ref.hash.html">Hash Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>