Skip to content

Commit 2efabe3

Browse files
mahuihuangeveryx
authored andcommitted
fix: Update SHA1 example to SHA256
1 parent 8ac0882 commit 2efabe3

File tree

7 files changed

+25
-25
lines changed

7 files changed

+25
-25
lines changed

PROGRESS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
- [x] Random Numbers->随机数
6666
- [x] Number Parsing->数字解析
6767
- [x] URL Parsing->URL 解析
68-
- [x] SHA1 Hashes->SHA1 哈希
68+
- [x] SHA256 Hashes->SHA256 散列
6969
- [x] Base64 Encoding->Base64 编码
7070
- [x] Reading Files->读文件
7171
- [x] Writing Files->写文件

examples.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Time Formatting / Parsing->时间的格式化和解析
5757
Random Numbers->随机数
5858
Number Parsing->数字解析
5959
URL Parsing->URL 解析
60-
SHA1 Hashes->SHA1 哈希
60+
SHA256 Hashes->SHA256 散列
6161
Base64 Encoding->Base64 编码
6262
Reading Files->读文件
6363
Writing Files->写文件

examples/sha1-hashes/sha1-hashes.hash

-2
This file was deleted.

examples/sha1-hashes/sha1-hashes.sh

-11
This file was deleted.

examples/sha1-hashes/sha1-hashes.go renamed to examples/sha256-hashes/sha256-hashes.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
// [_SHA1 散列(hash)_](http://en.wikipedia.org/wiki/SHA-1)
1+
// [_SHA256 散列(hash)_](https://en.wikipedia.org/wiki/SHA-2)
22
// 经常用于生成二进制文件或者文本块的短标识。
3-
// 例如,[git 版本控制系统](http://git-scm.com/) 大量的使用了 SHA1
4-
// 来标识受版本控制的文件和目录。
5-
// 这是 Go 中如何进行 SHA1 散列计算的例子。
3+
// 例如,TLS/SSL 证书使用 SHA256 来计算一个证书的签名。
4+
// 这是 Go 中如何进行 SHA256 散列计算的例子。
65

76
package main
87

98
// Go 在多个 `crypto/*` 包中实现了一系列散列函数。
109
import (
11-
"crypto/sha1"
10+
"crypto/sha256"
1211
"fmt"
1312
)
1413

1514
func main() {
16-
s := "sha1 this string"
15+
s := "sha256 this string"
1716

18-
// 产生一个散列值的方式是 `sha1.New()`,`sha1.Write(bytes)`,
19-
// 然后 `sha1.Sum([]byte{})`。这里我们从一个新的散列开始。
20-
h := sha1.New()
17+
// 这里我们从一个新的散列开始。
18+
h := sha256.New()
2119

2220
// 写入要处理的字节。如果是一个字符串,
2321
// 需要使用 `[]byte(s)` 将其强制转换成字节数组。
@@ -27,7 +25,7 @@ func main() {
2725
// 可以用来给现有的字符切片追加额外的字节切片:但是一般都不需要这样做。
2826
bs := h.Sum(nil)
2927

30-
// SHA1 值经常以 16 进制输出,例如在 git commit 中。
28+
// SHA256 值经常以 16 进制输出,例如在 git commit 中。
3129
// 我们这里也使用 `%x` 来将散列结果格式化为 16 进制字符串。
3230
fmt.Println(s)
3331
fmt.Printf("%x\n", bs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
51bb346b04a875eeccb49d576ec5e1866827e867
2+
C6xVtmaC-23
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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)。

0 commit comments

Comments
 (0)