From fa7e46c88481b06420191460e47d9c9c512a1f94 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 26 Jun 2013 13:14:11 -0700 Subject: [PATCH] crypto/sha512: provide top-level Sum512 and Sum384 functions Makes it easy to ask the simple question, what is the hash of this data? Also fix the commentary and prints in Sum256. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/10630043 --- src/pkg/crypto/sha256/sha256.go | 2 +- src/pkg/crypto/sha256/sha256_test.go | 2 +- src/pkg/crypto/sha512/sha512.go | 30 +++++++++++++++++++++++++--- src/pkg/crypto/sha512/sha512_test.go | 8 ++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/pkg/crypto/sha256/sha256.go b/src/pkg/crypto/sha256/sha256.go index 90e71cd61b..d69ed24a3b 100644 --- a/src/pkg/crypto/sha256/sha256.go +++ b/src/pkg/crypto/sha256/sha256.go @@ -179,7 +179,7 @@ func (d *digest) checkSum() [Size]byte { return digest } -// Sum returns the SHA256 checksum of the data. +// Sum256 returns the SHA256 checksum of the data. func Sum256(data []byte) [Size]byte { var d digest d.Reset() diff --git a/src/pkg/crypto/sha256/sha256_test.go b/src/pkg/crypto/sha256/sha256_test.go index 0dc8886252..bb1ec3b162 100644 --- a/src/pkg/crypto/sha256/sha256_test.go +++ b/src/pkg/crypto/sha256/sha256_test.go @@ -90,7 +90,7 @@ func TestGolden(t *testing.T) { g := golden[i] s := fmt.Sprintf("%x", Sum256([]byte(g.in))) if s != g.out { - t.Fatalf("Sum function: sha256(%s) = %s want %s", g.in, s, g.out) + t.Fatalf("Sum256 function: sha256(%s) = %s want %s", g.in, s, g.out) } c := New() for j := 0; j < 3; j++ { diff --git a/src/pkg/crypto/sha512/sha512.go b/src/pkg/crypto/sha512/sha512.go index 4aec529385..6825c22acd 100644 --- a/src/pkg/crypto/sha512/sha512.go +++ b/src/pkg/crypto/sha512/sha512.go @@ -135,7 +135,14 @@ func (d0 *digest) Sum(in []byte) []byte { // Make a copy of d0 so that caller can keep writing and summing. d := new(digest) *d = *d0 + hash := d.checkSum() + if d.is384 { + return append(in, hash[:Size384]...) + } + return append(in, hash[:]...) +} +func (d *digest) checkSum() [Size]byte { // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128. len := d.len var tmp [128]byte @@ -158,10 +165,8 @@ func (d0 *digest) Sum(in []byte) []byte { } h := d.h[:] - size := Size if d.is384 { h = d.h[:6] - size = Size384 } var digest [Size]byte @@ -176,5 +181,24 @@ func (d0 *digest) Sum(in []byte) []byte { digest[i*8+7] = byte(s) } - return append(in, digest[:size]...) + return digest +} + +// Sum returns the SHA512 checksum of the data. +func Sum512(data []byte) [Size]byte { + var d digest + d.Reset() + d.Write(data) + return d.checkSum() +} + +// Sum384 returns the SHA384 checksum of the data. +func Sum384(data []byte) (sum384 [Size384]byte) { + var d digest + d.is384 = true + d.Reset() + d.Write(data) + sum := d.checkSum() + copy(sum384[:], sum[:Size384]) + return } diff --git a/src/pkg/crypto/sha512/sha512_test.go b/src/pkg/crypto/sha512/sha512_test.go index 6eafb1b5fa..167c20ad07 100644 --- a/src/pkg/crypto/sha512/sha512_test.go +++ b/src/pkg/crypto/sha512/sha512_test.go @@ -88,6 +88,10 @@ var golden384 = []sha512Test{ func TestGolden(t *testing.T) { for i := 0; i < len(golden); i++ { g := golden[i] + s := fmt.Sprintf("%x", Sum512([]byte(g.in))) + if s != g.out { + t.Fatalf("Sum512 function: sha512(%s) = %s want %s", g.in, s, g.out) + } c := New() for j := 0; j < 3; j++ { if j < 2 { @@ -106,6 +110,10 @@ func TestGolden(t *testing.T) { } for i := 0; i < len(golden384); i++ { g := golden384[i] + s := fmt.Sprintf("%x", Sum384([]byte(g.in))) + if s != g.out { + t.Fatalf("Sum384 function: sha384(%s) = %s want %s", g.in, s, g.out) + } c := New384() for j := 0; j < 3; j++ { if j < 2 { -- 2.48.1