<!DOCTYPE html> <html> <head> <meta http-eqiv="content-type" content="text/html;charset=utf-8"> <title>Go by Example: SHA1 Hashes</title> <link rel=stylesheet href="site.css"> </head> <script type="text/javascript"> if (window.location.host == "gobyexample.com") { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-34996217-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); } </script> <body> <div class="example" id="sha1-hashes"> <h2><a href="./">Go by Example</a>: SHA1 Hashes</h2> <table> <tr> <td class="docs"> <p><a href="http://en.wikipedia.org/wiki/SHA-1"><em>SHA1 hashes</em></a> are frequently used to compute short identities for binary or text blobs. For example, the <a href="http://git-scm.com/">git revision control system</a> uses SHA1s extensively to identify versioned files and directories. Here’s how to compute SHA1 hashes in Go.</p> </td> <td class="code empty leading"> </td> </tr> <tr> <td class="docs"> </td> <td class="code leading"> <a href="http://play.golang.org/p/YUaWWEeB4U"><img title="Run code" src="play.png" class="run" /></a> <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span> </pre></div> </td> </tr> <tr> <td class="docs"> <p>Go implements several hash functions in various <code>crypto/*</code> packages.</p> </td> <td class="code leading"> <div class="highlight"><pre><span class="kn">import</span> <span class="s">"crypto/sha1"</span> <span class="kn">import</span> <span class="s">"fmt"</span> </pre></div> </td> </tr> <tr> <td class="docs"> </td> <td class="code leading"> <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span> <span class="nx">s</span> <span class="o">:=</span> <span class="s">"sha1 this string"</span> </pre></div> </td> </tr> <tr> <td class="docs"> <p>The pattern for generating a hash is <code>sha1.New()</code>, <code>sha1.Write(bytes)</code>, then <code>sha1.Sum([]byte{})</code>. Here we start with a new hash.</p> </td> <td class="code leading"> <div class="highlight"><pre> <span class="nx">h</span> <span class="o">:=</span> <span class="nx">sha1</span><span class="p">.</span><span class="nx">New</span><span class="p">()</span> </pre></div> </td> </tr> <tr> <td class="docs"> <p><code>Write</code> expects bytes. If you have a string <code>s</code>, use <code>[]byte(s)</code> to coerce it to bytes.</p> </td> <td class="code leading"> <div class="highlight"><pre> <span class="nx">h</span><span class="p">.</span><span class="nx">Write</span><span class="p">([]</span><span class="nb">byte</span><span class="p">(</span><span class="nx">s</span><span class="p">))</span> </pre></div> </td> </tr> <tr> <td class="docs"> <p>This gets the finalized hash result as a byte slice. The argument to <code>Sum</code> can be used to append to an existing byte slice: it usually isn’t needed.</p> </td> <td class="code leading"> <div class="highlight"><pre> <span class="nx">bs</span> <span class="o">:=</span> <span class="nx">h</span><span class="p">.</span><span class="nx">Sum</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span> </pre></div> </td> </tr> <tr> <td class="docs"> <p>SHA1 values are often printed in hex, for example in git commits. Use the <code>%x</code> format verb to convert a hash results to a hex string.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">"%x\n"</span><span class="p">,</span> <span class="nx">bs</span><span class="p">)</span> <span class="p">}</span> </pre></div> </td> </tr> </table> <table> <tr> <td class="docs"> <p>Running the program computes the hash and prints it in a human-readable hex format.</p> </td> <td class="code leading"> <div class="highlight"><pre><span class="gp">$</span> go run sha1-hashes.go <span class="go">sha1 this string</span> <span class="go">cf23df2207d99a74fbe169e3eba035e633b65d94</span> </pre></div> </td> </tr> <tr> <td class="docs"> <p>You can compute other hashes using a similar pattern to the one shown above. For example, to compute MD5 hashes import <code>crypto/md5</code> and use <code>md5.New()</code>.</p> </td> <td class="code empty leading"> </td> </tr> <tr> <td class="docs"> <p>Note that if you need cryptographically secure hashes, you should carefully research <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function">hash strength</a>!</p> </td> <td class="code empty"> </td> </tr> </table> <p class="next"> Next example: <a href="base64-encoding">Base64 Encoding</a>. </p> <p class="footer"> by <a href="https://twitter.com/mmcgrana">@mmcgrana</a> | <a href="mailto:mmcgrana@gmail.com">feedback</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/sha1-hashes">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p> </div> </body> </html>