diff options
Diffstat (limited to 'libgo/go/hash')
-rw-r--r-- | libgo/go/hash/adler32/adler32.go | 13 | ||||
-rw-r--r-- | libgo/go/hash/crc32/crc32.go | 13 | ||||
-rw-r--r-- | libgo/go/hash/crc64/crc64.go | 21 | ||||
-rw-r--r-- | libgo/go/hash/fnv/fnv.go | 53 | ||||
-rw-r--r-- | libgo/go/hash/fnv/fnv_test.go | 12 | ||||
-rw-r--r-- | libgo/go/hash/hash.go | 6 |
6 files changed, 67 insertions, 51 deletions
diff --git a/libgo/go/hash/adler32/adler32.go b/libgo/go/hash/adler32/adler32.go index 10bed2f..8103a89 100644 --- a/libgo/go/hash/adler32/adler32.go +++ b/libgo/go/hash/adler32/adler32.go @@ -71,14 +71,13 @@ func (d *digest) Write(p []byte) (nn int, err error) { func (d *digest) Sum32() uint32 { return finish(d.a, d.b) } -func (d *digest) Sum() []byte { - p := make([]byte, 4) +func (d *digest) Sum(in []byte) []byte { s := d.Sum32() - p[0] = byte(s >> 24) - p[1] = byte(s >> 16) - p[2] = byte(s >> 8) - p[3] = byte(s) - return p + in = append(in, byte(s>>24)) + in = append(in, byte(s>>16)) + in = append(in, byte(s>>8)) + in = append(in, byte(s)) + return in } // Checksum returns the Adler-32 checksum of data. diff --git a/libgo/go/hash/crc32/crc32.go b/libgo/go/hash/crc32/crc32.go index 5980ec0..557fab8 100644 --- a/libgo/go/hash/crc32/crc32.go +++ b/libgo/go/hash/crc32/crc32.go @@ -119,14 +119,13 @@ func (d *digest) Write(p []byte) (n int, err error) { func (d *digest) Sum32() uint32 { return d.crc } -func (d *digest) Sum() []byte { - p := make([]byte, 4) +func (d *digest) Sum(in []byte) []byte { s := d.Sum32() - p[0] = byte(s >> 24) - p[1] = byte(s >> 16) - p[2] = byte(s >> 8) - p[3] = byte(s) - return p + in = append(in, byte(s>>24)) + in = append(in, byte(s>>16)) + in = append(in, byte(s>>8)) + in = append(in, byte(s)) + return in } // Checksum returns the CRC-32 checksum of data diff --git a/libgo/go/hash/crc64/crc64.go b/libgo/go/hash/crc64/crc64.go index 42e53c3..e5c1db4 100644 --- a/libgo/go/hash/crc64/crc64.go +++ b/libgo/go/hash/crc64/crc64.go @@ -75,18 +75,17 @@ func (d *digest) Write(p []byte) (n int, err error) { func (d *digest) Sum64() uint64 { return d.crc } -func (d *digest) Sum() []byte { - p := make([]byte, 8) +func (d *digest) Sum(in []byte) []byte { s := d.Sum64() - p[0] = byte(s >> 56) - p[1] = byte(s >> 48) - p[2] = byte(s >> 40) - p[3] = byte(s >> 32) - p[4] = byte(s >> 24) - p[5] = byte(s >> 16) - p[6] = byte(s >> 8) - p[7] = byte(s) - return p + in = append(in, byte(s>>56)) + in = append(in, byte(s>>48)) + in = append(in, byte(s>>40)) + in = append(in, byte(s>>32)) + in = append(in, byte(s>>24)) + in = append(in, byte(s>>16)) + in = append(in, byte(s>>8)) + in = append(in, byte(s)) + return in } // Checksum returns the CRC-64 checksum of data diff --git a/libgo/go/hash/fnv/fnv.go b/libgo/go/hash/fnv/fnv.go index ce3ed0d..2c8a251 100644 --- a/libgo/go/hash/fnv/fnv.go +++ b/libgo/go/hash/fnv/fnv.go @@ -8,7 +8,6 @@ package fnv import ( - "encoding/binary" "hash" ) @@ -105,26 +104,46 @@ func (s *sum32a) Size() int { return 4 } func (s *sum64) Size() int { return 8 } func (s *sum64a) Size() int { return 8 } -func (s *sum32) Sum() []byte { - a := make([]byte, 4) - binary.BigEndian.PutUint32(a, uint32(*s)) - return a +func (s *sum32) Sum(in []byte) []byte { + v := uint32(*s) + in = append(in, byte(v>>24)) + in = append(in, byte(v>>16)) + in = append(in, byte(v>>8)) + in = append(in, byte(v)) + return in } -func (s *sum32a) Sum() []byte { - a := make([]byte, 4) - binary.BigEndian.PutUint32(a, uint32(*s)) - return a +func (s *sum32a) Sum(in []byte) []byte { + v := uint32(*s) + in = append(in, byte(v>>24)) + in = append(in, byte(v>>16)) + in = append(in, byte(v>>8)) + in = append(in, byte(v)) + return in } -func (s *sum64) Sum() []byte { - a := make([]byte, 8) - binary.BigEndian.PutUint64(a, uint64(*s)) - return a +func (s *sum64) Sum(in []byte) []byte { + v := uint64(*s) + in = append(in, byte(v>>56)) + in = append(in, byte(v>>48)) + in = append(in, byte(v>>40)) + in = append(in, byte(v>>32)) + in = append(in, byte(v>>24)) + in = append(in, byte(v>>16)) + in = append(in, byte(v>>8)) + in = append(in, byte(v)) + return in } -func (s *sum64a) Sum() []byte { - a := make([]byte, 8) - binary.BigEndian.PutUint64(a, uint64(*s)) - return a +func (s *sum64a) Sum(in []byte) []byte { + v := uint64(*s) + in = append(in, byte(v>>56)) + in = append(in, byte(v>>48)) + in = append(in, byte(v>>40)) + in = append(in, byte(v>>32)) + in = append(in, byte(v>>24)) + in = append(in, byte(v>>16)) + in = append(in, byte(v>>8)) + in = append(in, byte(v)) + return in } diff --git a/libgo/go/hash/fnv/fnv_test.go b/libgo/go/hash/fnv/fnv_test.go index 429230c..17454de 100644 --- a/libgo/go/hash/fnv/fnv_test.go +++ b/libgo/go/hash/fnv/fnv_test.go @@ -72,7 +72,7 @@ func testGolden(t *testing.T, hash hash.Hash, gold []golden) { if done != len(g.text) { t.Fatalf("wrote only %d out of %d bytes", done, len(g.text)) } - if actual := hash.Sum(); !bytes.Equal(g.sum, actual) { + if actual := hash.Sum(nil); !bytes.Equal(g.sum, actual) { t.Errorf("hash(%q) = 0x%x want 0x%x", g.text, actual, g.sum) } } @@ -97,26 +97,26 @@ func TestIntegrity64a(t *testing.T) { func testIntegrity(t *testing.T, h hash.Hash) { data := []byte{'1', '2', 3, 4, 5} h.Write(data) - sum := h.Sum() + sum := h.Sum(nil) if size := h.Size(); size != len(sum) { t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum)) } - if a := h.Sum(); !bytes.Equal(sum, a) { + if a := h.Sum(nil); !bytes.Equal(sum, a) { t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a) } h.Reset() h.Write(data) - if a := h.Sum(); !bytes.Equal(sum, a) { + if a := h.Sum(nil); !bytes.Equal(sum, a) { t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a) } h.Reset() h.Write(data[:2]) h.Write(data[2:]) - if a := h.Sum(); !bytes.Equal(sum, a) { + if a := h.Sum(nil); !bytes.Equal(sum, a) { t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a) } @@ -162,6 +162,6 @@ func benchmark(b *testing.B, h hash.Hash) { for todo := b.N; todo != 0; todo-- { h.Reset() h.Write(data) - h.Sum() + h.Sum(nil) } } diff --git a/libgo/go/hash/hash.go b/libgo/go/hash/hash.go index 3536c0b..0d7765d 100644 --- a/libgo/go/hash/hash.go +++ b/libgo/go/hash/hash.go @@ -13,9 +13,9 @@ type Hash interface { // It never returns an error. io.Writer - // Sum returns the current hash, without changing the - // underlying hash state. - Sum() []byte + // Sum appends the current hash in the same manner as append(), without + // changing the underlying hash state. + Sum(in []byte) []byte // Reset resets the hash to one with zero bytes written. Reset() |