-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash.go
50 lines (38 loc) · 949 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
package cdb
import "hash"
const (
start = 5381 // Initial cdb checksum value.
)
// digest represents the partial evaluation of a checksum.
type digest struct {
h uint32
}
func (d *digest) Reset() { d.h = start }
// New returns a new hash computing the cdb checksum.
func cdbHash() hash.Hash32 {
d := new(digest)
d.Reset()
return d
}
func (d *digest) Size() int { return 4 }
func update(h uint32, p []byte) uint32 {
for i := 0; i < len(p); i++ {
h = ((h << 5) + h) ^ uint32(p[i])
}
return h
}
func (d *digest) Write(p []byte) (int, error) {
d.h = update(d.h, p)
return len(p), nil
}
func (d *digest) Sum32() uint32 { return d.h }
func (d *digest) Sum(in []byte) []byte {
s := d.Sum32()
in = append(in, byte(s>>24))
in = append(in, byte(s>>16))
in = append(in, byte(s>>8))
in = append(in, byte(s))
return in
}
func (d *digest) BlockSize() int { return 1 }
func checksum(data []byte) uint32 { return update(start, data) }