forked from gobyexample-cn/gobyexample-cn.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha256-hashes.html
201 lines (152 loc) · 6.99 KB
/
sha256-hashes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: SHA256 散列</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="sha256-hashes">
<h2><a href="./">Go by Example 中文版</a>: SHA256 散列</h2>
<table>
<tr>
<td class="docs">
<p><a href="https://en.wikipedia.org/wiki/SHA-2"><em>SHA256 散列(hash)</em></a>
经常用于生成二进制文件或者文本块的短标识。
例如,TLS/SSL 证书使用 SHA256 来计算一个证书的签名。
这是 Go 中如何进行 SHA256 散列计算的例子。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/C6xVtmaC-23"><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 在多个 <code>crypto/*</code> 包中实现了一系列散列函数。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kn">import</span> <span class="p">(</span>
<span class="s">"crypto/sha256"</span>
<span class="s">"fmt"</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">"sha256 this string"</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>这里我们从一个新的散列开始。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">h</span> <span class="o">:=</span> <span class="nx">sha256</span><span class="p">.</span><span class="nf">New</span><span class="p">()</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>写入要处理的字节。如果是一个字符串,
需要使用 <code>[]byte(s)</code> 将其强制转换成字节数组。</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><code>Sum</code> 得到最终的散列值的字符切片。<code>Sum</code> 接收一个参数,
可以用来给现有的字符切片追加额外的字节切片:但是一般都不需要这样做。</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>SHA256 值经常以 16 进制输出,例如在 git commit 中。
我们这里也使用 <code>%x</code> 来将散列结果格式化为 16 进制字符串。</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">"%x\n"</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>运行程序计算散列值,并以可读的 16 进制格式输出。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go run sha256-hashes.go
<span class="go">sha256 this string
</span><span class="go">1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a...</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>你可以使用和上面相似的方式来计算其他形式的散列值。
例如,计算 SHA512 散列,
引入 <code>crypto/sha512</code> 并使用 <code>sha512.New()</code> 方法。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
<p>注意,如果你需要密码学上的安全散列,你需要仔细的研究一下
<a href="https://en.wikipedia.org/wiki/Cryptographic_hash_function">加密散列函数</a>。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="base64-encoding.html">Base64 编码</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"crypto/sha256\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func main() {\u000A s :\u003D \"sha256 this string\"\u000A');codeLines.push(' h :\u003D sha256.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>