-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathhash.go
79 lines (68 loc) · 1.31 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package jsondiff
import (
"encoding/binary"
"hash/maphash"
"math"
"slices"
)
type hasher struct {
mh maphash.Hash
}
func (h *hasher) digest(val interface{}, sort bool) uint64 {
h.mh.Reset()
h.hash(val, sort)
return h.mh.Sum64()
}
func (h *hasher) hash(i interface{}, sort bool) {
switch v := i.(type) {
case string:
_, _ = h.mh.WriteString(v)
case bool:
if v {
_ = h.mh.WriteByte('1')
} else {
_ = h.mh.WriteByte('0')
}
case float64:
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], math.Float64bits(v))
_, _ = h.mh.Write(buf[:])
case nil:
_ = h.mh.WriteByte('0')
case []interface{}:
if sort {
h.sortArray(v)
}
for _, e := range v {
h.hash(e, sort)
}
case map[string]interface{}:
keys := make([]string, 0, len(v))
// Extract keys first, and sort them
// in lexicographical order.
for k := range v {
keys = append(keys, k)
}
sortStrings(keys)
for _, k := range keys {
_, _ = h.mh.WriteString(k)
h.hash(v[k], sort)
}
}
}
func (h *hasher) sortArray(a []interface{}) {
h1 := hasher{}
h2 := hasher{}
h1.mh.SetSeed(h.mh.Seed())
h2.mh.SetSeed(h.mh.Seed())
slices.SortStableFunc(a, func(a1, a2 interface{}) int {
d1 := h1.digest(a1, true)
d2 := h2.digest(a2, true)
if d1 > d2 {
return 1
} else if d1 < d2 {
return -1
}
return 0
})
}