mirror of https://github.com/golang/go.git
crypto/sha256: add examples for New, Sum256
The goal for these examples is to show how to mirror the functionality of the sha256sum Unix utility, a common checksumming tool, using the Go standard library. Add a newline at the end of the input, so users will get the same output if they type `echo 'hello world' | sha256sum`, since the builtin shell echo appends a newline by default. Also use hex output (instead of the shorter base64) since this is the default output encoding for shasum/sha256sum. Change-Id: I0036874b3cc5ba85432bfcb86f81b51c4e0238fd Reviewed-on: https://go-review.googlesource.com/24868 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
3968ac2caf
commit
448d3952af
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sha256_test
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ExampleSum256() {
|
||||
sum := sha256.Sum256([]byte("hello world\n"))
|
||||
fmt.Printf("%x", sum)
|
||||
// Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
|
||||
}
|
||||
|
||||
func ExampleNew() {
|
||||
h := sha256.New()
|
||||
h.Write([]byte("hello world\n"))
|
||||
fmt.Printf("%x", h.Sum(nil))
|
||||
// Output: a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
|
||||
}
|
||||
Loading…
Reference in New Issue