1
2
3
4
5 package sha256_test
6
7 import (
8 "crypto/sha256"
9 "fmt"
10 "io"
11 "log"
12 "os"
13 )
14
15 func ExampleSum256() {
16 sum := sha256.Sum256([]byte("hello world\n"))
17 fmt.Printf("%x", sum)
18
19 }
20
21 func ExampleNew() {
22 h := sha256.New()
23 h.Write([]byte("hello world\n"))
24 fmt.Printf("%x", h.Sum(nil))
25
26 }
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 }
42
View as plain text