Commit 31ba8550 authored by Emmanuel Odeke's avatar Emmanuel Odeke Committed by Brad Fitzpatrick

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent ca4089ad
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"io" "io"
"log"
"os"
) )
func ExampleNew() { func ExampleNew() {
...@@ -23,3 +25,18 @@ func ExampleSum() { ...@@ -23,3 +25,18 @@ func ExampleSum() {
fmt.Printf("%x", md5.Sum(data)) fmt.Printf("%x", md5.Sum(data))
// Output: b0804ec967f48520697662a204f5fe72 // 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))
}
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"crypto/sha1" "crypto/sha1"
"fmt" "fmt"
"io" "io"
"log"
"os"
) )
func ExampleNew() { func ExampleNew() {
...@@ -23,3 +25,18 @@ func ExampleSum() { ...@@ -23,3 +25,18 @@ func ExampleSum() {
fmt.Printf("% x", sha1.Sum(data)) 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 // 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))
}
...@@ -7,6 +7,9 @@ package sha256_test ...@@ -7,6 +7,9 @@ package sha256_test
import ( import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"io"
"log"
"os"
) )
func ExampleSum256() { func ExampleSum256() {
...@@ -21,3 +24,18 @@ func ExampleNew() { ...@@ -21,3 +24,18 @@ func ExampleNew() {
fmt.Printf("%x", h.Sum(nil)) fmt.Printf("%x", h.Sum(nil))
// Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447 // 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))
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment