1
0
mirror of https://github.com/golang/go synced 2024-11-18 13:04:46 -07:00

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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Emmanuel Odeke 2016-09-18 16:51:02 -07:00 committed by Brad Fitzpatrick
parent ca4089ad62
commit 31ba855014
3 changed files with 52 additions and 0 deletions

View File

@ -8,6 +8,8 @@ import (
"crypto/md5"
"fmt"
"io"
"log"
"os"
)
func ExampleNew() {
@ -23,3 +25,18 @@ func ExampleSum() {
fmt.Printf("%x", md5.Sum(data))
// Output: b0804ec967f48520697662a204f5fe72
}
func ExampleNew_file() {
f, err := os.Open("file.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
fmt.Printf("%x", h.Sum(nil))
}

View File

@ -8,6 +8,8 @@ import (
"crypto/sha1"
"fmt"
"io"
"log"
"os"
)
func ExampleNew() {
@ -23,3 +25,18 @@ func ExampleSum() {
fmt.Printf("% x", sha1.Sum(data))
// Output: af 06 49 23 bb f2 30 15 96 aa c4 c2 73 ba 32 17 8e bc 4a 96
}
func ExampleNew_file() {
f, err := os.Open("file.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := sha1.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
fmt.Printf("% x", h.Sum(nil))
}

View File

@ -7,6 +7,9 @@ package sha256_test
import (
"crypto/sha256"
"fmt"
"io"
"log"
"os"
)
func ExampleSum256() {
@ -21,3 +24,18 @@ func ExampleNew() {
fmt.Printf("%x", h.Sum(nil))
// Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
}
func ExampleNew_file() {
f, err := os.Open("file.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
fmt.Printf("%x", h.Sum(nil))
}