-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.go
More file actions
61 lines (55 loc) · 919 Bytes
/
hash.go
File metadata and controls
61 lines (55 loc) · 919 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package consistentHash
import (
"sync"
)
const (
offset32 = 2166136261
prime32 = 16777619
)
type (
HashFunc func(data []byte) uint32
fnv1a uint32
)
func newFnv1a() *fnv1a {
var h fnv1a = offset32
return &h
}
func (s *fnv1a) Write(data []byte) (int, error) {
h := *s
for _, c := range data {
h ^= fnv1a(c)
h *= prime32
}
*s = h
return len(data), nil
}
func (s *fnv1a) Sum(in []byte) []byte {
v := uint32(*s)
return append(in,
byte(v>>24),
byte(v>>16),
byte(v>>8),
byte(v),
)
}
func (s *fnv1a) Sum32() uint32 {
return uint32(*s)
}
func (s *fnv1a) Reset() { *s = offset32 }
func (s *fnv1a) Size() int { return 4 }
func (s *fnv1a) BlockSize() int { return 1 }
var (
hPool = sync.Pool{
New: func() any {
return newFnv1a()
},
}
)
func Fnv1a(data []byte) uint32 {
h := hPool.Get().(*fnv1a)
_, _ = h.Write(data)
v := h.Sum32()
h.Reset()
hPool.Put(h)
return v
}