gobyexample/public/sha1-hashes
Oleg Butuzov 646c8b39da bugfix (mmcgrana/gobyexample#294) sha1 sum calculator
This bugfix implements correct way to calculate source sha1 hash, before it will changed,
by propagation of unchanged sources.

This commit will also include regenerated static files and *.hash files.
2019-10-22 14:27:02 +03:00

211 lines
7.0 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" />
<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="p">(</span>
<span class="s">&quot;crypto/sha1&quot;</span>
<span class="s">&quot;fmt&quot;</span>
<span class="p">)</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">&quot;sha1 this string&quot;</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&rsquo;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">&quot;%x\n&quot;</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://markmcgranaghan.com">Mark McGranaghan</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>
<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 := \"sha1 this string\"\u000A');codeLines.push(' h := sha1.New()\u000A');codeLines.push(' h.Write([]byte(s))\u000A');codeLines.push(' bs := 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>