From: Kevin Burke Date: Tue, 12 Jul 2016 23:54:36 +0000 (-0700) Subject: crypto/sha256: add examples for New, Sum256 X-Git-Tag: go1.8beta1~1547 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=448d3952af2e60eab7fddd0d8a7b8e0ea6905073;p=gostls13.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 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/crypto/sha256/example_test.go b/src/crypto/sha256/example_test.go new file mode 100644 index 0000000000..0eb70b711b --- /dev/null +++ b/src/crypto/sha256/example_test.go @@ -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 +}