Add hashing package

This commit is contained in:
Alexander Neumann 2017-01-22 12:43:36 +01:00
parent 30ff7413be
commit c93f79f0f3
4 changed files with 207 additions and 0 deletions

View file

@ -0,0 +1,29 @@
package hashing
import (
"hash"
"io"
)
// Reader hashes all data read from the underlying reader.
type Reader struct {
r io.Reader
h hash.Hash
}
// NewReader returns a new Reader that uses the hash h.
func NewReader(r io.Reader, h hash.Hash) *Reader {
return &Reader{
h: h,
r: io.TeeReader(r, h),
}
}
func (h *Reader) Read(p []byte) (int, error) {
return h.r.Read(p)
}
// Sum returns the hash of the data read so far.
func (h *Reader) Sum(d []byte) []byte {
return h.h.Sum(d)
}

View file

@ -0,0 +1,73 @@
package hashing
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"io"
"io/ioutil"
"testing"
)
func TestReader(t *testing.T) {
tests := []int{5, 23, 2<<18 + 23, 1 << 20}
for _, size := range tests {
data := make([]byte, size)
_, err := io.ReadFull(rand.Reader, data)
if err != nil {
t.Fatalf("ReadFull: %v", err)
}
expectedHash := sha256.Sum256(data)
rd := NewReader(bytes.NewReader(data), sha256.New())
n, err := io.Copy(ioutil.Discard, rd)
if err != nil {
t.Fatal(err)
}
if n != int64(size) {
t.Errorf("Reader: invalid number of bytes written: got %d, expected %d",
n, size)
}
resultingHash := rd.Sum(nil)
if !bytes.Equal(expectedHash[:], resultingHash) {
t.Errorf("Reader: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash)
}
}
}
func BenchmarkReader(b *testing.B) {
buf := make([]byte, 1<<22)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
b.Fatal(err)
}
expectedHash := sha256.Sum256(buf)
b.SetBytes(int64(len(buf)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
rd := NewReader(bytes.NewReader(buf), sha256.New())
n, err := io.Copy(ioutil.Discard, rd)
if err != nil {
b.Fatal(err)
}
if n != int64(len(buf)) {
b.Errorf("Reader: invalid number of bytes written: got %d, expected %d",
n, len(buf))
}
resultingHash := rd.Sum(nil)
if !bytes.Equal(expectedHash[:], resultingHash) {
b.Errorf("Reader: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash)
}
}
}

View file

@ -0,0 +1,31 @@
package hashing
import (
"hash"
"io"
)
// Writer transparently hashes all data while writing it to the underlying writer.
type Writer struct {
w io.Writer
h hash.Hash
}
// NewWriter wraps the writer w and feeds all data written to the hash h.
func NewWriter(w io.Writer, h hash.Hash) *Writer {
return &Writer{
h: h,
w: io.MultiWriter(w, h),
}
}
// Write wraps the write method of the underlying writer and also hashes all data.
func (h *Writer) Write(p []byte) (int, error) {
n, err := h.w.Write(p)
return n, err
}
// Sum returns the hash of all data written so far.
func (h *Writer) Sum(d []byte) []byte {
return h.h.Sum(d)
}

View file

@ -0,0 +1,74 @@
package hashing
import (
"bytes"
"crypto/rand"
"crypto/sha256"
"io"
"io/ioutil"
"testing"
)
func TestWriter(t *testing.T) {
tests := []int{5, 23, 2<<18 + 23, 1 << 20}
for _, size := range tests {
data := make([]byte, size)
_, err := io.ReadFull(rand.Reader, data)
if err != nil {
t.Fatalf("ReadFull: %v", err)
}
expectedHash := sha256.Sum256(data)
wr := NewWriter(ioutil.Discard, sha256.New())
n, err := io.Copy(wr, bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
if n != int64(size) {
t.Errorf("Writer: invalid number of bytes written: got %d, expected %d",
n, size)
}
resultingHash := wr.Sum(nil)
if !bytes.Equal(expectedHash[:], resultingHash) {
t.Errorf("Writer: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash)
}
}
}
func BenchmarkWriter(b *testing.B) {
buf := make([]byte, 1<<22)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
b.Fatal(err)
}
expectedHash := sha256.Sum256(buf)
b.SetBytes(int64(len(buf)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
wr := NewWriter(ioutil.Discard, sha256.New())
n, err := io.Copy(wr, bytes.NewReader(buf))
if err != nil {
b.Fatal(err)
}
if n != int64(len(buf)) {
b.Errorf("Writer: invalid number of bytes written: got %d, expected %d",
n, len(buf))
}
resultingHash := wr.Sum(nil)
if !bytes.Equal(expectedHash[:], resultingHash) {
b.Errorf("Writer: hashes do not match: expected %02x, got %02x",
expectedHash, resultingHash)
}
}
}