-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.go
50 lines (39 loc) · 847 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 id
import (
"fmt"
"golang.org/x/crypto/sha3"
)
type HashType int
const (
XXH32 HashType = iota
SHA3
)
var DefaultHash = XXH32
func Hash(c interface{}) Id { return HashAs(DefaultHash, c) }
// TODO: Want to add add/switch to xxhash
func HashAs(hashType HashType, c interface{}) Id {
switch hashType {
case SHA3:
return sha3Hash(c)
default: // XXH32
return xxh32Hash(c)
}
}
func xxh32Hash(c interface{}) Id {
xxhash32 := NewXXHash32()
h := xxhash32.Sum([]byte(fmt.Sprintf("%v", c)))
return Id{
stringValue: fmt.Sprintf("%x", h[:8]),
byteSliceValue: h[:8],
uint32Value: u32(h),
}
}
func sha3Hash(c interface{}) Id {
h := make([]byte, 64)
sha3.ShakeSum256(h, []byte(fmt.Sprintf("%v", c)))
return Id{
stringValue: fmt.Sprintf("%x", h),
byteSliceValue: h[:12],
uint32Value: u32(h[:12]),
}
}