]> Cypherpunks repositories - gostls13.git/commitdiff
hash: document that Sum does not change hash state
authorRuss Cox <rsc@golang.org>
Tue, 19 Jan 2010 18:50:04 +0000 (10:50 -0800)
committerRuss Cox <rsc@golang.org>
Tue, 19 Jan 2010 18:50:04 +0000 (10:50 -0800)
crypto/*: implement and test proper Sum

Fixes #216.

R=agl1
CC=golang-dev
https://golang.org/cl/186210

src/pkg/crypto/md4/md4.go
src/pkg/crypto/md4/md4_test.go
src/pkg/crypto/md5/md5.go
src/pkg/crypto/md5/md5_test.go
src/pkg/crypto/sha1/sha1.go
src/pkg/crypto/sha1/sha1_test.go
src/pkg/crypto/sha256/sha256.go
src/pkg/crypto/sha256/sha256_test.go
src/pkg/hash/hash.go

index 6096ab9975361b5289f2b9dbba9cf6601a44cb3a..793cb16fda8516179103b427842ee7179fcb49c0 100644 (file)
@@ -76,7 +76,11 @@ func (d *digest) Write(p []byte) (nn int, err os.Error) {
        return
 }
 
-func (d *digest) Sum() []byte {
+func (d0 *digest) Sum() []byte {
+       // Make a copy of d0, so that caller can keep writing and summing.
+       d := new(digest)
+       *d = *d0
+
        // Padding.  Add a 1 bit and 0 bits until 56 bytes mod 64.
        len := d.len
        var tmp [64]byte
index 9cab80c63eaadff6c73a3d899992a641cfdca047..b883e64590616ef1be69d3d4eb623b15e19ee0a5 100644 (file)
@@ -53,12 +53,17 @@ func TestGolden(t *testing.T) {
        for i := 0; i < len(golden); i++ {
                g := golden[i]
                c := New()
-               for j := 0; j < 2; j++ {
-                       io.WriteString(c, g.in)
+               for j := 0; j < 3; j++ {
+                       if j < 2 {
+                               io.WriteString(c, g.in)
+                       } else {
+                               io.WriteString(c, g.in[0:len(g.in)/2])
+                               c.Sum()
+                               io.WriteString(c, g.in[len(g.in)/2:])
+                       }
                        s := fmt.Sprintf("%x", c.Sum())
                        if s != g.out {
-                               t.Errorf("md4[%d](%s) = %s want %s", j, g.in, s, g.out)
-                               t.FailNow()
+                               t.Fatalf("md4[%d](%s) = %s want %s", j, g.in, s, g.out)
                        }
                        c.Reset()
                }
index fd0984a418adc2294e78474a81ab83578ca15447..90774af6b7f6840834a70d12f1f7d4534b97cd96 100644 (file)
@@ -76,7 +76,11 @@ func (d *digest) Write(p []byte) (nn int, err os.Error) {
        return
 }
 
-func (d *digest) Sum() []byte {
+func (d0 *digest) Sum() []byte {
+       // Make a copy of d0 so that caller can keep writing and summing.
+       d := new(digest)
+       *d = *d0
+
        // Padding.  Add a 1 bit and 0 bits until 56 bytes mod 64.
        len := d.len
        var tmp [64]byte
index 7d5737b267e59143e89a992aaffdd6a95b976d8b..f6c29383707d3f2e3d786dfe2363730c9a20e764 100644 (file)
@@ -53,12 +53,17 @@ func TestGolden(t *testing.T) {
        for i := 0; i < len(golden); i++ {
                g := golden[i]
                c := New()
-               for j := 0; j < 2; j++ {
-                       io.WriteString(c, g.in)
+               for j := 0; j < 3; j++ {
+                       if j < 2 {
+                               io.WriteString(c, g.in)
+                       } else {
+                               io.WriteString(c, g.in[0:len(g.in)/2])
+                               c.Sum()
+                               io.WriteString(c, g.in[len(g.in)/2:])
+                       }
                        s := fmt.Sprintf("%x", c.Sum())
                        if s != g.out {
-                               t.Errorf("md5[%d](%s) = %s want %s", j, g.in, s, g.out)
-                               t.FailNow()
+                               t.Fatalf("md5[%d](%s) = %s want %s", j, g.in, s, g.out)
                        }
                        c.Reset()
                }
index 7209041ee1a4f3d875d89f180843fd56007007c6..98f0a066755ffa7e0cbdfb3c3a5cfe0f352eba4e 100644 (file)
@@ -78,7 +78,11 @@ func (d *digest) Write(p []byte) (nn int, err os.Error) {
        return
 }
 
-func (d *digest) Sum() []byte {
+func (d0 *digest) Sum() []byte {
+       // Make a copy of d0 so that caller can keep writing and summing.
+       d := new(digest)
+       *d = *d0
+
        // Padding.  Add a 1 bit and 0 bits until 56 bytes mod 64.
        len := d.len
        var tmp [64]byte
index 8d4485282fe365131201806643c2a4cac3814719..f18c7b09670d21a36334a256caf26f95f72aaeb1 100644 (file)
@@ -55,12 +55,17 @@ func TestGolden(t *testing.T) {
        for i := 0; i < len(golden); i++ {
                g := golden[i]
                c := New()
-               for j := 0; j < 2; j++ {
-                       io.WriteString(c, g.in)
+               for j := 0; j < 3; j++ {
+                       if j < 2 {
+                               io.WriteString(c, g.in)
+                       } else {
+                               io.WriteString(c, g.in[0:len(g.in)/2])
+                               c.Sum()
+                               io.WriteString(c, g.in[len(g.in)/2:])
+                       }
                        s := fmt.Sprintf("%x", c.Sum())
                        if s != g.out {
-                               t.Errorf("sha1[%d](%s) = %s want %s", j, g.in, s, g.out)
-                               t.FailNow()
+                               t.Fatalf("sha1[%d](%s) = %s want %s", j, g.in, s, g.out)
                        }
                        c.Reset()
                }
index bacefc5637acb93fa721550d24fa68ad81923510..df18e5fb269b20979a291a458c59e2f60af12aeb 100644 (file)
@@ -84,7 +84,11 @@ func (d *digest) Write(p []byte) (nn int, err os.Error) {
        return
 }
 
-func (d *digest) Sum() []byte {
+func (d0 *digest) Sum() []byte {
+       // Make a copy of d0 so that caller can keep writing and summing.
+       d := new(digest)
+       *d = *d0
+
        // Padding.  Add a 1 bit and 0 bits until 56 bytes mod 64.
        len := d.len
        var tmp [64]byte
index 29c0bce601d4270c35f2b6059a3b0d88008324ee..4d0be6257d6fba3de8d1b8e50a40272f90eb0c56 100644 (file)
@@ -55,12 +55,17 @@ func TestGolden(t *testing.T) {
        for i := 0; i < len(golden); i++ {
                g := golden[i]
                c := New()
-               for j := 0; j < 2; j++ {
-                       io.WriteString(c, g.in)
+               for j := 0; j < 3; j++ {
+                       if j < 2 {
+                               io.WriteString(c, g.in)
+                       } else {
+                               io.WriteString(c, g.in[0:len(g.in)/2])
+                               c.Sum()
+                               io.WriteString(c, g.in[len(g.in)/2:])
+                       }
                        s := fmt.Sprintf("%x", c.Sum())
                        if s != g.out {
-                               t.Errorf("sha256[%d](%s) = %s want %s", j, g.in, s, g.out)
-                               t.FailNow()
+                               t.Fatalf("sha256[%d](%s) = %s want %s", j, g.in, s, g.out)
                        }
                        c.Reset()
                }
index 470e9a36c08f7c004c23096277f9e342a9bf0ae6..f5c08d3602ef8c917f38dfae363ee4bec68e4ff5 100644 (file)
@@ -7,13 +7,20 @@ package hash
 import "io"
 
 // Hash is the common interface implemented by all hash functions.
-// The Write method never returns an error.
-// Sum returns the bytes of integer hash codes in big-endian order.
 type Hash interface {
+       // Write adds more data to the running hash.
+       // It never returns an error.
        io.Writer
+
+       // Sum returns the current hash, without changing the
+       // underlying hash state.
        Sum() []byte
+
+       // Reset resets the hash to one with zero bytes written.
        Reset()
-       Size() int // number of bytes Sum returns
+
+       // Size returns the number of bytes Sum will return.
+       Size() int
 }
 
 // Hash32 is the common interface implemented by all 32-bit hash functions.