-
Notifications
You must be signed in to change notification settings - Fork 5
/
hash.go
51 lines (38 loc) · 815 Bytes
/
hash.go
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
package cdb
import "hash"
const (
startingHash = 5381
size = 4
)
// NewHash returns new instance of hash.Hash32
func NewHash() hash.Hash32 {
return &hashImpl{startingHash}
}
// hashImpl implements hash.Hash32 described http://cr.yp.to/cdb/cdb.txt
type hashImpl struct {
uint32
}
func (h *hashImpl) Sum32() uint32 {
return h.uint32
}
func (h *hashImpl) Write(data []byte) (int, error) {
val := h.uint32
for _, c := range data {
val = ((val << 5) + val) ^ uint32(c)
}
h.uint32 = val
return len(data), nil
}
func (h *hashImpl) Reset() {
h.uint32 = startingHash
}
func (h *hashImpl) Sum(b []byte) []byte {
s := h.Sum32()
return append(b, byte(s>>24), byte(s>>16), byte(s>>8), byte(s))
}
func (h *hashImpl) BlockSize() int {
return 1
}
func (h *hashImpl) Size() int {
return size
}