Skip to content

Commit 31ba855

Browse files
odeke-embradfitz
authored andcommitted
crypto/md5, crypto/sha1, crypto/sha256: add examples for checksumming a file
Updates #16360. Change-Id: I75714d2b5f095fe39fd81edfa6dd9e44d7c44da1 Reviewed-on: https://go-review.googlesource.com/29375 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent ca4089a commit 31ba855

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/crypto/md5/example_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"crypto/md5"
99
"fmt"
1010
"io"
11+
"log"
12+
"os"
1113
)
1214

1315
func ExampleNew() {
@@ -23,3 +25,18 @@ func ExampleSum() {
2325
fmt.Printf("%x", md5.Sum(data))
2426
// Output: b0804ec967f48520697662a204f5fe72
2527
}
28+
29+
func ExampleNew_file() {
30+
f, err := os.Open("file.txt")
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
defer f.Close()
35+
36+
h := md5.New()
37+
if _, err := io.Copy(h, f); err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
fmt.Printf("%x", h.Sum(nil))
42+
}

src/crypto/sha1/example_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"crypto/sha1"
99
"fmt"
1010
"io"
11+
"log"
12+
"os"
1113
)
1214

1315
func ExampleNew() {
@@ -23,3 +25,18 @@ func ExampleSum() {
2325
fmt.Printf("% x", sha1.Sum(data))
2426
// Output: af 06 49 23 bb f2 30 15 96 aa c4 c2 73 ba 32 17 8e bc 4a 96
2527
}
28+
29+
func ExampleNew_file() {
30+
f, err := os.Open("file.txt")
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
defer f.Close()
35+
36+
h := sha1.New()
37+
if _, err := io.Copy(h, f); err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
fmt.Printf("% x", h.Sum(nil))
42+
}

src/crypto/sha256/example_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package sha256_test
77
import (
88
"crypto/sha256"
99
"fmt"
10+
"io"
11+
"log"
12+
"os"
1013
)
1114

1215
func ExampleSum256() {
@@ -21,3 +24,18 @@ func ExampleNew() {
2124
fmt.Printf("%x", h.Sum(nil))
2225
// Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
2326
}
27+
28+
func ExampleNew_file() {
29+
f, err := os.Open("file.txt")
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
defer f.Close()
34+
35+
h := sha256.New()
36+
if _, err := io.Copy(h, f); err != nil {
37+
log.Fatal(err)
38+
}
39+
40+
fmt.Printf("%x", h.Sum(nil))
41+
}

0 commit comments

Comments
 (0)