gobyexample/public/sha1-hashes
2021-09-02 10:26:17 -07:00

210 lines
6.9 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: SHA1 Hashes</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'url-parsing';
}
if (e.key == "ArrowRight") {
window.location.href = 'base64-encoding';
}
}
</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&rsquo;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/XLftf8Gvj4y"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Go implements several hash functions in various
<code>crypto/*</code> packages.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kn">import</span> <span class="p">(</span>
<span class="s">&#34;crypto/sha1&#34;</span>
<span class="s">&#34;fmt&#34;</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">s</span> <span class="o">:=</span> <span class="s">&#34;sha1 this string&#34;</span>
</pre>
</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">
<pre class="chroma">
<span class="nx">h</span> <span class="o">:=</span> <span class="nx">sha1</span><span class="p">.</span><span class="nf">New</span><span class="p">()</span>
</pre>
</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">
<pre class="chroma">
<span class="nx">h</span><span class="p">.</span><span class="nf">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>
</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&rsquo;t needed.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">bs</span> <span class="o">:=</span> <span class="nx">h</span><span class="p">.</span><span class="nf">Sum</span><span class="p">(</span><span class="kc">nil</span><span class="p">)</span>
</pre>
</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">
<pre class="chroma">
<span class="nx">fmt</span><span class="p">.</span><span class="nf">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="nf">Printf</span><span class="p">(</span><span class="s">&#34;%x\n&#34;</span><span class="p">,</span> <span class="nx">bs</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</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">
<pre class="chroma">
<span class="gp">$</span> go run sha1-hashes.go
<span class="go">sha1 this string
</span><span class="go">cf23df2207d99a74fbe169e3eba035e633b65d94</span></pre>
</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://markmcgranaghan.com">Mark McGranaghan</a> and <a href="https://eli.thegreenplace.net">Eli Bendersky</a> | <a href="https://github.com/mmcgrana/gobyexample">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"crypto/sha1\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A s :\u003D \"sha1 this string\"\u000A');codeLines.push(' h :\u003D sha1.New()\u000A');codeLines.push(' h.Write([]byte(s))\u000A');codeLines.push(' bs :\u003D h.Sum(nil)\u000A');codeLines.push(' fmt.Println(s)\u000A fmt.Printf(\"%x\\n\", bs)\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>