forked from benlaurie/objecthash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjecthash.go
179 lines (162 loc) · 3.32 KB
/
objecthash.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package objecthash
import "bytes"
import "crypto/sha256"
import "encoding/json"
import "fmt"
import "sort"
import "golang.org/x/text/unicode/norm"
const hashLength int = sha256.Size
func hash(t string, b []byte) [hashLength]byte {
//fmt.Printf("%x %x\n", []byte(t), b)
h := sha256.New()
h.Write([]byte(t))
h.Write(b)
// FIXME: Seriously, WTF?
var r []byte
r = h.Sum(r)
var rr [hashLength]byte
copy(rr[:], r)
//fmt.Printf("= %x\n", rr)
return rr;
}
// FIXME: if What You Hash Is What You Get, then this needs to be safe
// to use as a set.
// Note: not actually safe to use as a set
type Set []interface{}
type sortableHashes [][hashLength]byte
func (h sortableHashes) Len() int { return len(h) }
func (h sortableHashes) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h sortableHashes) Less(i, j int) bool { return bytes.Compare(h[i][:], h[j][:]) < 0 }
func hashSet(s Set) [hashLength]byte {
h := make([][hashLength]byte, len(s))
for n, e := range s {
h[n] = ObjectHash(e)
}
sort.Sort(sortableHashes(h))
b := new(bytes.Buffer)
var prev [hashLength]byte
for _, hh := range h {
if hh != prev {
b.Write(hh[:])
}
prev = hh
}
return hash(`s`, b.Bytes())
}
func hashList(l []interface{}) [hashLength]byte {
h := new(bytes.Buffer)
for _, o := range l {
b := ObjectHash(o)
h.Write(b[:])
}
return hash(`l`, h.Bytes())
}
func hashUnicode(s string) [hashLength]byte {
return hash(`u`, norm.NFC.Bytes([]byte(s)))
}
type hashEntry struct {
khash [hashLength]byte
vhash [hashLength]byte
}
type byKHash []hashEntry
func (h byKHash) Len() int { return len(h) }
func (h byKHash) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h byKHash) Less(i, j int) bool { return bytes.Compare(h[i].khash[:],
h[j].khash[:]) < 0 }
func hashDict(d map[string]interface {}) [hashLength]byte {
e := make([]hashEntry, len(d))
n := 0
for k, v := range d {
e[n].khash = ObjectHash(k)
e[n].vhash = ObjectHash(v)
n++
}
sort.Sort(byKHash(e))
h := new(bytes.Buffer)
for _, ee := range e {
h.Write(ee.khash[:])
h.Write(ee.vhash[:])
}
return hash(`d`, h.Bytes())
}
func floatNormalize(f float64) (s string) {
// sign
s = `+`
if f < 0 {
s = `-`
f = -f
}
// exponent
e := 0
for f > 1 {
f /= 2
e++
}
for f <= .5 {
f *= 2
e--
}
s += fmt.Sprintf("%d:", e)
// mantissa
if f > 1 || f <= .5 {
panic(f)
}
for f != 0 {
if f >= 1 {
s += `1`
f -= 1
} else {
s += `0`
}
if (f >= 1) {
panic(f)
}
if (len(s) >= 1000) {
panic(s)
}
f *= 2
}
return
}
func hashFloat(f float64) [hashLength]byte {
return hash(`f`, []byte(floatNormalize(f)))
}
func hashInt(i int) [hashLength]byte {
return hash(`i`, []byte(fmt.Sprintf("%d", i)))
}
func hashBool(b bool) [hashLength]byte {
bb := []byte(`0`)
if b {
bb = []byte(`1`)
}
return hash(`b`, bb)
}
func ObjectHash(o interface{}) [hashLength]byte {
switch v := o.(type) {
case []interface{}:
return hashList(v)
case string:
return hashUnicode(v)
case map[string]interface {}:
return hashDict(v)
case float64:
return hashFloat(v)
case nil:
return hash(`n`, []byte(``))
case int:
return hashInt(v)
case Set:
return hashSet(v)
case bool:
return hashBool(v)
default:
panic(o)
}
}
func CommonJSONHash(j string) [hashLength]byte {
var f interface{}
if err := json.Unmarshal([]byte(j), &f); err != nil {
panic(err)
}
return ObjectHash(f)
}