File tree 7 files changed +25
-25
lines changed
7 files changed +25
-25
lines changed Original file line number Diff line number Diff line change 65
65
- [x] Random Numbers->随机数
66
66
- [x] Number Parsing->数字解析
67
67
- [x] URL Parsing->URL 解析
68
- - [x] SHA1 Hashes->SHA1 哈希
68
+ - [x] SHA256 Hashes->SHA256 散列
69
69
- [x] Base64 Encoding->Base64 编码
70
70
- [x] Reading Files->读文件
71
71
- [x] Writing Files->写文件
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ Time Formatting / Parsing->时间的格式化和解析
57
57
Random Numbers->随机数
58
58
Number Parsing->数字解析
59
59
URL Parsing->URL 解析
60
- SHA1 Hashes->SHA1 哈希
60
+ SHA256 Hashes->SHA256 散列
61
61
Base64 Encoding->Base64 编码
62
62
Reading Files->读文件
63
63
Writing Files->写文件
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- // [_SHA1 散列(hash)_](http ://en.wikipedia.org/wiki/SHA-1 )
1
+ // [_SHA256 散列(hash)_](https ://en.wikipedia.org/wiki/SHA-2 )
2
2
// 经常用于生成二进制文件或者文本块的短标识。
3
- // 例如,[git 版本控制系统](http://git-scm.com/) 大量的使用了 SHA1
4
- // 来标识受版本控制的文件和目录。
5
- // 这是 Go 中如何进行 SHA1 散列计算的例子。
3
+ // 例如,TLS/SSL 证书使用 SHA256 来计算一个证书的签名。
4
+ // 这是 Go 中如何进行 SHA256 散列计算的例子。
6
5
7
6
package main
8
7
9
8
// Go 在多个 `crypto/*` 包中实现了一系列散列函数。
10
9
import (
11
- "crypto/sha1 "
10
+ "crypto/sha256 "
12
11
"fmt"
13
12
)
14
13
15
14
func main () {
16
- s := "sha1 this string"
15
+ s := "sha256 this string"
17
16
18
- // 产生一个散列值的方式是 `sha1.New()`,`sha1.Write(bytes)`,
19
- // 然后 `sha1.Sum([]byte{})`。这里我们从一个新的散列开始。
20
- h := sha1 .New ()
17
+ // 这里我们从一个新的散列开始。
18
+ h := sha256 .New ()
21
19
22
20
// 写入要处理的字节。如果是一个字符串,
23
21
// 需要使用 `[]byte(s)` 将其强制转换成字节数组。
@@ -27,7 +25,7 @@ func main() {
27
25
// 可以用来给现有的字符切片追加额外的字节切片:但是一般都不需要这样做。
28
26
bs := h .Sum (nil )
29
27
30
- // SHA1 值经常以 16 进制输出,例如在 git commit 中。
28
+ // SHA256 值经常以 16 进制输出,例如在 git commit 中。
31
29
// 我们这里也使用 `%x` 来将散列结果格式化为 16 进制字符串。
32
30
fmt .Println (s )
33
31
fmt .Printf ("%x\n " , bs )
Original file line number Diff line number Diff line change
1
+ 51bb346b04a875eeccb49d576ec5e1866827e867
2
+ C6xVtmaC-23
Original file line number Diff line number Diff line change
1
+ # 运行程序计算散列值,并以可读的 16 进制格式输出。
2
+ $ go run sha256-hashes.go
3
+ sha256 this string
4
+ 1af1dfa857bf1d8814fe1af8983c18080019922e557f15a8a...
5
+
6
+
7
+ # 你可以使用和上面相似的方式来计算其他形式的散列值。
8
+ # 例如,计算 SHA512 散列,
9
+ # 引入 `crypto/sha512` 并使用 `sha512.New()` 方法。
10
+
11
+
12
+ # 注意,如果你需要密码学上的安全散列,你需要仔细的研究一下
13
+ # [加密散列函数](https://en.wikipedia.org/wiki/Cryptographic_hash_function)。
You can’t perform that action at this time.
0 commit comments