|
|
@ -20,30 +20,68 @@ import (
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func calcHash(v interface{}) string {
|
|
|
|
// hasher is reusable state for hashing a value.
|
|
|
|
h := sha256.New()
|
|
|
|
// Get one via hasherPool.
|
|
|
|
b := bufio.NewWriterSize(h, h.BlockSize())
|
|
|
|
type hasher struct {
|
|
|
|
scratch := make([]byte, 0, 128)
|
|
|
|
h hash.Hash
|
|
|
|
printTo(b, v, scratch)
|
|
|
|
bw *bufio.Writer
|
|
|
|
b.Flush()
|
|
|
|
scratch [128]byte
|
|
|
|
scratch = h.Sum(scratch[:0])
|
|
|
|
}
|
|
|
|
// The first sha256.Size bytes contain the hash.
|
|
|
|
|
|
|
|
// Hex-encode that into the next sha256.Size*2 bytes.
|
|
|
|
// newHasher initializes a new hasher, for use by hasherPool.
|
|
|
|
src := scratch[:sha256.Size]
|
|
|
|
func newHasher() *hasher {
|
|
|
|
dst := scratch[sha256.Size:cap(scratch)]
|
|
|
|
h := &hasher{h: sha256.New()}
|
|
|
|
n := hex.Encode(dst, src)
|
|
|
|
h.bw = bufio.NewWriterSize(h.h, h.h.BlockSize())
|
|
|
|
return string(dst[:n])
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateHash sets last to the hash of v and reports whether its value changed.
|
|
|
|
// Hash returns the raw SHA-256 (not hex) of v.
|
|
|
|
|
|
|
|
func (h *hasher) Hash(v interface{}) (hash [sha256.Size]byte) {
|
|
|
|
|
|
|
|
h.bw.Flush()
|
|
|
|
|
|
|
|
h.h.Reset()
|
|
|
|
|
|
|
|
printTo(h.bw, v, h.scratch[:])
|
|
|
|
|
|
|
|
h.bw.Flush()
|
|
|
|
|
|
|
|
h.h.Sum(hash[:0])
|
|
|
|
|
|
|
|
return hash
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var hasherPool = &sync.Pool{
|
|
|
|
|
|
|
|
New: func() interface{} { return newHasher() },
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hash returns the raw SHA-256 hash of v.
|
|
|
|
|
|
|
|
func Hash(v interface{}) [sha256.Size]byte {
|
|
|
|
|
|
|
|
hasher := hasherPool.Get().(*hasher)
|
|
|
|
|
|
|
|
hasherPool.Put(hasher)
|
|
|
|
|
|
|
|
return hasher.Hash(v)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateHash sets last to the hex-encoded hash of v and reports whether its value changed.
|
|
|
|
func UpdateHash(last *string, v ...interface{}) (changed bool) {
|
|
|
|
func UpdateHash(last *string, v ...interface{}) (changed bool) {
|
|
|
|
sig := calcHash(v)
|
|
|
|
sum := Hash(v)
|
|
|
|
if *last != sig {
|
|
|
|
if sha256EqualHex(sum, *last) {
|
|
|
|
*last = sig
|
|
|
|
// unchanged.
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*last = hex.EncodeToString(sum[:])
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sha256EqualHex reports whether hx is the hex encoding of sum.
|
|
|
|
|
|
|
|
func sha256EqualHex(sum [sha256.Size]byte, hx string) bool {
|
|
|
|
|
|
|
|
if len(hx) != len(sum)*2 {
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const hextable = "0123456789abcdef"
|
|
|
|
|
|
|
|
j := 0
|
|
|
|
|
|
|
|
for _, v := range sum {
|
|
|
|
|
|
|
|
if hx[j] != hextable[v>>4] || hx[j+1] != hextable[v&0x0f] {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
j += 2
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func printTo(w *bufio.Writer, v interface{}, scratch []byte) {
|
|
|
|
func printTo(w *bufio.Writer, v interface{}, scratch []byte) {
|
|
|
|
print(w, reflect.ValueOf(v), make(map[uintptr]bool), scratch)
|
|
|
|
print(w, reflect.ValueOf(v), make(map[uintptr]bool), scratch)
|
|
|
|