]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/sha256: add examples for New, Sum256
authorKevin Burke <kev@inburke.com>
Tue, 12 Jul 2016 23:54:36 +0000 (16:54 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 31 Aug 2016 05:13:48 +0000 (05:13 +0000)
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>

src/crypto/sha256/example_test.go [new file with mode: 0644]

diff --git a/src/crypto/sha256/example_test.go b/src/crypto/sha256/example_test.go
new file mode 100644 (file)
index 0000000..0eb70b7
--- /dev/null
@@ -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
+}