util/deephash: make hash type opaque

The fact that Hash returns a [sha256.Size]byte leaks details about
the underlying hash implementation. This could very well be any other
hashing algorithm with a possible different block size.

Abstract this implementation detail away by declaring an opaque type
that is comparable. While we are changing the signature of UpdateHash,
rename it to just Update to reduce stutter (e.g., deephash.Update).

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai
2021-07-19 22:49:51 -07:00
parent b6d70203d3
commit 9a0c8bdd20
4 changed files with 27 additions and 49 deletions

View File

@ -7,8 +7,6 @@ package deephash
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"reflect"
"testing"
@ -233,7 +231,7 @@ func BenchmarkTailcfgNode(b *testing.B) {
}
func TestExhaustive(t *testing.T) {
seen := make(map[[sha256.Size]byte]bool)
seen := make(map[Sum]bool)
for i := 0; i < 100000; i++ {
s := Hash(i)
if seen[s] {
@ -243,19 +241,6 @@ func TestExhaustive(t *testing.T) {
}
}
func TestSHA256EqualHex(t *testing.T) {
for i := 0; i < 1000; i++ {
sum := Hash(i)
hx := hex.EncodeToString(sum[:])
if !sha256EqualHex(sum, hx) {
t.Fatal("didn't match, should've")
}
if sha256EqualHex(sum, hx[:len(hx)-1]) {
t.Fatal("matched on wrong length")
}
}
}
// verify this doesn't loop forever, as it used to (Issue 2340)
func TestMapCyclicFallback(t *testing.T) {
type T struct {