/
hash.go
380 lines (330 loc) · 7.34 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package util
import (
"crypto/sha1"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"fmt"
"golang.org/x/crypto/ripemd160"
"hash"
"io"
"math/big"
"github.com/copernet/copernicus/log"
)
const (
Hash256Size = 32
MaxHashStringSize = Hash256Size * 2
Hash160Size = 20
)
type Hash [Hash256Size]byte
var HashZero = Hash{0x0000000000000000000000000000000000000000000000000000000000000000}
var HashOne = Hash{0x0000000000000000000000000000000000000000000000000000000000000001}
func Sha256Bytes(b []byte) []byte {
hash := sha256.Sum256(b)
return hash[:Hash256Size]
}
func Sha256Hash(b []byte) Hash {
return Hash(sha256.Sum256(b))
}
func DoubleSha256Bytes(b []byte) []byte {
first := sha256.Sum256(b)
second := sha256.Sum256(first[:])
return second[:]
}
func DoubleSha256Hash(b []byte) Hash {
first := sha256.Sum256(b)
return Hash(sha256.Sum256(first[:Hash256Size]))
}
// Hash160 calculates the hash ripemd160(sha256(b)).
func Hash160(buf []byte) []byte {
return calcHash(calcHash(buf, sha256.New()), ripemd160.New())
}
func Ripemd160(buf []byte) []byte {
return calcHash(buf, ripemd160.New())
}
// Calculate the hash of hasher over buf.
func calcHash(buf []byte, hasher hash.Hash) []byte {
hasher.Write(buf)
return hasher.Sum(nil)
}
func Sha1(buf []byte) [20]byte {
return sha1.Sum(buf)
}
func (hash Hash) String() string {
bytes := hash.GetCloneBytes()
for i := 0; i < Hash256Size/2; i++ {
bytes[i], bytes[Hash256Size-1-i] = bytes[Hash256Size-1-i], bytes[i]
}
return hex.EncodeToString(bytes)
}
func (hash *Hash) SerializeSize() uint32 {
return hash.EncodeSize()
}
func (hash *Hash) Serialize(w io.Writer) (int, error) {
return hash.Encode(w)
}
func (hash *Hash) Unserialize(r io.Reader) (int, error) {
return hash.Decode(r)
}
func (hash *Hash) EncodeSize() uint32 {
return Hash256Size
}
func (hash *Hash) Encode(w io.Writer) (int, error) {
length, err := w.Write(hash[:])
if length != Hash256Size || err != nil {
log.Alert("hash.Unserialize err: ", length, err)
return length, err
}
return length, err
}
func (hash *Hash) Decode(r io.Reader) (int, error) {
length, err := io.ReadFull(r, hash[:])
if length != Hash256Size || err != nil {
log.Alert("hash.Unserialize err: ", length, err)
return length, err
}
return length, err
}
func (hash *Hash) GetCloneBytes() []byte {
bytes := make([]byte, Hash256Size)
copy(bytes, hash[:])
return bytes
}
func (hash *Hash) ToBigInt() *big.Int {
return new(big.Int).SetBytes(hash.GetCloneBytes())
}
func (hash *Hash) Cmp(other *Hash) int {
if hash == nil || other == nil {
return 0
} else if hash == nil {
return -1
} else if other == nil {
return 1
}
return hash.ToBigInt().Cmp(other.ToBigInt())
}
func (hash *Hash) IsEqual(target *Hash) bool {
if hash == nil && target == nil {
return true
}
if hash == nil || target == nil {
return false
}
return *hash == *target
}
func (hash *Hash) IsNull() bool {
for _, item := range hash {
if item != 0 {
return false
}
}
return true
}
func HashFromString(hexString string) *Hash {
hash, err := GetHashFromStr(hexString)
if err != nil {
panic(err)
}
return hash
}
func GetHashFromStr(hashStr string) (hash *Hash, err error) {
hash = new(Hash)
bytes, err := GetHashBytesFromStr(hashStr)
if err != nil {
return
}
copy(hash[:], bytes)
return
}
func GetHashBytesFromStr(src string) (bytes []byte, err error) {
if len(src) > MaxHashStringSize {
return nil, fmt.Errorf("max hash string length is %v bytes", MaxHashStringSize)
}
var srcBytes []byte
var srcLen = len(src)
if srcLen%2 == 0 {
srcBytes = []byte(src)
} else {
srcBytes = make([]byte, 1+srcLen)
srcBytes[0] = '0'
copy(srcBytes[1:], src)
}
var reversedHash = make([]byte, Hash256Size)
_, err = hex.Decode(reversedHash[Hash256Size-hex.DecodedLen(len(srcBytes)):], srcBytes)
if err != nil {
return
}
bytes = make([]byte, Hash256Size)
for i, b := range reversedHash[:Hash256Size/2] {
bytes[i], bytes[Hash256Size-1-i] = reversedHash[Hash256Size-1-i], b
}
return
}
func rotl(x uint64, b uint8) uint64 {
return (x << b) | (x >> (64 - b))
}
func sipRound(rn []uint64) {
rn[0] += rn[1]
rn[1] = rotl(rn[1], 13)
rn[1] ^= rn[0]
rn[0] = rotl(rn[0], 32)
rn[2] += rn[3]
rn[3] = rotl(rn[3], 16)
rn[3] ^= rn[2]
rn[0] += rn[3]
rn[3] = rotl(rn[3], 21)
rn[3] ^= rn[0]
rn[2] += rn[1]
rn[1] = rotl(rn[1], 17)
rn[1] ^= rn[2]
rn[2] = rotl(rn[2], 32)
}
func SipHash(k0, k1 uint64, hash []byte) uint64 {
d := binary.LittleEndian.Uint64(hash[0:8])
v0 := uint64(0x736f6d6570736575) ^ k0
v1 := uint64(0x646f72616e646f6d) ^ k1
v2 := uint64(0x6c7967656e657261) ^ k0
v3 := uint64(0x7465646279746573) ^ k1 ^ d
rn := []uint64{v0, v1, v2, v3}
sipRound(rn)
sipRound(rn)
rn[0] ^= d
d = binary.LittleEndian.Uint64(hash[8:16])
rn[3] ^= d
sipRound(rn)
sipRound(rn)
rn[0] ^= d
d = binary.LittleEndian.Uint64(hash[16:24])
rn[3] ^= d
sipRound(rn)
sipRound(rn)
rn[0] ^= d
d = binary.LittleEndian.Uint64(hash[24:32])
rn[3] ^= d
sipRound(rn)
sipRound(rn)
rn[0] ^= d
rn[3] ^= uint64(4) << 59
sipRound(rn)
sipRound(rn)
rn[0] ^= uint64(4) << 59
rn[2] ^= 0xFF
sipRound(rn)
sipRound(rn)
sipRound(rn)
sipRound(rn)
return rn[0] ^ rn[1] ^ rn[2] ^ rn[3]
}
func SipHashExtra(k0, k1 uint64, hash []byte, extra uint32) uint64 {
d := binary.LittleEndian.Uint64(hash[0:8])
v0 := uint64(0x736f6d6570736575) ^ k0
v1 := uint64(0x646f72616e646f6d) ^ k1
v2 := uint64(0x6c7967656e657261) ^ k0
v3 := uint64(0x7465646279746573) ^ k1 ^ d
rn := []uint64{v0, v1, v2, v3}
sipRound(rn)
sipRound(rn)
rn[0] ^= d
d = binary.LittleEndian.Uint64(hash[8:16])
rn[3] ^= d
sipRound(rn)
sipRound(rn)
rn[0] ^= d
d = binary.LittleEndian.Uint64(hash[16:24])
rn[3] ^= d
sipRound(rn)
sipRound(rn)
rn[0] ^= d
d = binary.LittleEndian.Uint64(hash[24:32])
rn[3] ^= d
sipRound(rn)
sipRound(rn)
rn[0] ^= d
d = (uint64(36) << 56) | uint64(extra)
rn[3] ^= d
sipRound(rn)
sipRound(rn)
rn[0] ^= d
rn[2] ^= 0xFF
sipRound(rn)
sipRound(rn)
sipRound(rn)
sipRound(rn)
return rn[0] ^ rn[1] ^ rn[2] ^ rn[3]
}
type SipHasher struct {
v [4]uint64
tmp uint64
count int
}
func NewSipHasher(k0, k1 uint64) *SipHasher {
return &SipHasher{
v: [4]uint64{
0: uint64(0x736f6d6570736575) ^ k0,
1: uint64(0x646f72616e646f6d) ^ k1,
2: uint64(0x6c7967656e657261) ^ k0,
3: uint64(0x7465646279746573) ^ k1,
},
}
}
func (sh *SipHasher) WriteUint64(data uint64) *SipHasher {
if sh == nil {
return nil
}
if sh.count%8 != 0 {
panic("expect multiple of 8 bytes have been written so far")
}
rn := make([]uint64, 4)
copy(rn, sh.v[:])
rn[3] ^= data
sipRound(rn)
sipRound(rn)
rn[0] ^= data
copy(sh.v[:], rn)
sh.count += 8
return sh
}
func (sh *SipHasher) Write(data []byte) *SipHasher {
if sh == nil {
return nil
}
t := sh.tmp
c := sh.count
size := len(data)
rn := make([]uint64, 4)
copy(rn, sh.v[:])
for i := 0; i < size; i++ {
t |= uint64(data[i]) << uint(8*(c%8))
c++
if c&7 == 0 {
rn[3] ^= t
sipRound(rn)
sipRound(rn)
rn[0] ^= t
t = 0
}
}
copy(sh.v[:], rn)
//sh.v[0] = rn[0]
//sh.v[1] = rn[1]
//sh.v[2] = rn[2]
//sh.v[3] = rn[3]
sh.count = c
sh.tmp = t
return sh
}
func (sh *SipHasher) Finalize() uint64 {
rn := make([]uint64, 4)
copy(rn, sh.v[:])
t := sh.tmp | (uint64(sh.count) << 56)
rn[3] ^= t
sipRound(rn)
sipRound(rn)
rn[0] ^= t
rn[2] ^= 0xff
sipRound(rn)
sipRound(rn)
sipRound(rn)
sipRound(rn)
return rn[0] ^ rn[1] ^ rn[2] ^ rn[3]
}