util/sha256x: rename Hash as Block512 (#5351)

Rename Hash as Block512 to indicate that this is a general-purpose
hash.Hash for any algorithm that operates on 512-bit block sizes.

While we rename the package as hashx in this commit,
a subsequent commit will move the sha256x package to hashx.
This is done separately to avoid confusing git.

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai
2022-08-16 09:49:48 -07:00
committed by GitHub
parent 44d62b65d0
commit f061d20c9d
4 changed files with 106 additions and 72 deletions

View File

@@ -34,7 +34,7 @@ import (
"time"
"unsafe"
"tailscale.com/util/sha256x"
hashx "tailscale.com/util/sha256x"
)
// There is much overlap between the theory of serialization and hashing.
@@ -82,7 +82,7 @@ const scratchSize = 128
// hasher is reusable state for hashing a value.
// Get one via hasherPool.
type hasher struct {
sha256x.Hash
hashx.Block512
scratch [scratchSize]byte
visitStack visitStack
}
@@ -111,6 +111,13 @@ func initSeed() {
seed = uint64(time.Now().UnixNano())
}
func (h *hasher) Reset() {
if h.Block512.Hash == nil {
h.Block512.Hash = sha256.New()
}
h.Block512.Reset()
}
func (h *hasher) sum() (s Sum) {
h.Sum(s.sum[:0])
return s

View File

@@ -531,7 +531,7 @@ func TestGetTypeHasher(t *testing.T) {
fn := getTypeInfo(va.Type()).hasher()
hb := &hashBuffer{Hash: sha256.New()}
h := new(hasher)
h.Hash.H = hb
h.Block512.Hash = hb
got := fn(h, va)
const ptrSize = 32 << uintptr(^uintptr(0)>>63)
if tt.out32 != "" && ptrSize == 32 {
@@ -628,7 +628,7 @@ func TestHashMapAcyclic(t *testing.T) {
v := addressableValue{reflect.ValueOf(&m).Elem()}
hb.Reset()
h := new(hasher)
h.Hash.H = hb
h.Block512.Hash = hb
h.hashMap(v, ti, false)
h.sum()
if got[string(hb.B)] {
@@ -648,7 +648,7 @@ func TestPrintArray(t *testing.T) {
x := T{X: [32]byte{1: 1, 31: 31}}
hb := &hashBuffer{Hash: sha256.New()}
h := new(hasher)
h.Hash.H = hb
h.Block512.Hash = hb
h.hashValue(addressableValue{reflect.ValueOf(&x).Elem()}, false)
h.sum()
const want = "\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1f"
@@ -669,7 +669,7 @@ func BenchmarkHashMapAcyclic(b *testing.B) {
ti := getTypeInfo(v.Type())
h := new(hasher)
h.Hash.H = hb
h.Block512.Hash = hb
for i := 0; i < b.N; i++ {
h.Reset()