-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhasher.go
More file actions
43 lines (35 loc) · 755 Bytes
/
hasher.go
File metadata and controls
43 lines (35 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package berghain
import "hash"
// zeroHasher provides a wrapper to create zero allocation hashes.
type zeroHasher struct {
h hash.Hash
buf []byte
}
func NewZeroHasher(h hash.Hash) hash.Hash {
return &zeroHasher{
h: h,
buf: make([]byte, 0, h.Size()),
}
}
func (zh *zeroHasher) Sum(b []byte) []byte {
if b != nil {
panic("zeroHasher does not support any parameter for Sum()")
}
if len(zh.buf) != 0 {
panic("invalid buffer state")
}
return zh.h.Sum(zh.buf)
}
func (zh *zeroHasher) Size() int {
return zh.h.Size()
}
func (zh *zeroHasher) BlockSize() int {
return zh.h.BlockSize()
}
func (zh *zeroHasher) Write(p []byte) (int, error) {
return zh.h.Write(p)
}
func (zh *zeroHasher) Reset() {
zh.buf = zh.buf[:0]
zh.h.Reset()
}