2015-03-24 07:04:45 +00:00
|
|
|
package sha512
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-21 06:44:08 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/rand" // To register the stdlib sha224 and sha256 algs.
|
|
|
|
"crypto/sha512"
|
|
|
|
"hash"
|
2015-03-24 07:04:45 +00:00
|
|
|
"io"
|
|
|
|
"testing"
|
2015-05-21 06:44:08 +00:00
|
|
|
|
|
|
|
"github.com/stevvooe/resumable"
|
2015-03-24 07:04:45 +00:00
|
|
|
)
|
|
|
|
|
2015-05-21 06:44:08 +00:00
|
|
|
func compareResumableHash(t *testing.T, newResumable func() hash.Hash, newStdlib func() hash.Hash) {
|
2015-03-24 07:04:45 +00:00
|
|
|
// Read 3 Kilobytes of random data into a buffer.
|
|
|
|
buf := make([]byte, 3*1024)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, buf); err != nil {
|
|
|
|
t.Fatalf("unable to load random data: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use two Hash objects to consume prefixes of the data. One will be
|
|
|
|
// snapshotted and resumed with each additional byte, then both will write
|
|
|
|
// that byte. The digests should be equal after each byte is digested.
|
2015-05-21 06:44:08 +00:00
|
|
|
resumableHasher := newResumable().(resumable.Hash)
|
|
|
|
stdlibHasher := newStdlib()
|
2015-03-24 07:04:45 +00:00
|
|
|
|
|
|
|
// First, assert that the initial distest is the same.
|
|
|
|
if !bytes.Equal(resumableHasher.Sum(nil), stdlibHasher.Sum(nil)) {
|
|
|
|
t.Fatalf("initial digests do not match: got %x, expected %x", resumableHasher.Sum(nil), stdlibHasher.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
multiWriter := io.MultiWriter(resumableHasher, stdlibHasher)
|
|
|
|
|
|
|
|
for i := 1; i <= len(buf); i++ {
|
|
|
|
|
|
|
|
// Write the next byte.
|
|
|
|
multiWriter.Write(buf[i-1 : i])
|
|
|
|
|
|
|
|
if !bytes.Equal(resumableHasher.Sum(nil), stdlibHasher.Sum(nil)) {
|
|
|
|
t.Fatalf("digests do not match: got %x, expected %x", resumableHasher.Sum(nil), stdlibHasher.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot, reset, and restore the chunk hasher.
|
|
|
|
hashState, err := resumableHasher.State()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to get state of hash function: %s", err)
|
|
|
|
}
|
|
|
|
resumableHasher.Reset()
|
|
|
|
if err := resumableHasher.Restore(hashState); err != nil {
|
|
|
|
t.Fatalf("unable to restorte state of hash function: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResumable(t *testing.T) {
|
2015-05-21 06:44:08 +00:00
|
|
|
compareResumableHash(t, New384, sha512.New384)
|
|
|
|
compareResumableHash(t, New, sha512.New)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResumableRegistered(t *testing.T) {
|
|
|
|
|
|
|
|
for _, hf := range []crypto.Hash{crypto.SHA384, crypto.SHA512} {
|
|
|
|
// make sure that the hash gets the resumable version from the global
|
|
|
|
// registry in crypto library.
|
|
|
|
h := hf.New()
|
|
|
|
|
|
|
|
if rh, ok := h.(resumable.Hash); !ok {
|
|
|
|
t.Fatalf("non-resumable hash function registered: %#v %#v", rh, crypto.SHA256)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-24 07:04:45 +00:00
|
|
|
}
|