-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnv.go
54 lines (43 loc) · 1.08 KB
/
fnv.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
52
53
54
package fnv
import (
fnvLib "hash/fnv"
"github.com/ThotaGopichandThota/gn-core2/hashing"
)
var _ hashing.Hasher = (*fnv)(nil)
// fnv is a fnv128a implementation of the hasher interface.
type fnv struct {
emptyHash []byte
}
// NewFnv initializes the empty hash and returns a new instance of the fnv hasher
func NewFnv() *fnv {
return &fnv{
emptyHash: computeEmptyHash(),
}
}
// Compute takes a string, and returns the fnv128a hash of that string
func (f *fnv) Compute(s string) []byte {
if len(s) == 0 {
return f.getEmptyHash()
}
h := fnvLib.New128a()
_, _ = h.Write([]byte(s))
return h.Sum(nil)
}
// Size returns the size, in number of bytes, of a fnv128a hash
func (f *fnv) Size() int {
return fnvLib.New128a().Size()
}
func (f *fnv) getEmptyHash() []byte {
hashCopy := make([]byte, len(f.emptyHash))
copy(hashCopy, f.emptyHash)
return hashCopy
}
func computeEmptyHash() []byte {
h := fnvLib.New128a()
_, _ = h.Write([]byte(""))
return h.Sum(nil)
}
// IsInterfaceNil returns true if there is no value under the interface
func (f *fnv) IsInterfaceNil() bool {
return f == nil
}