-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathhashers.go
More file actions
349 lines (312 loc) · 9.92 KB
/
hashers.go
File metadata and controls
349 lines (312 loc) · 9.92 KB
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
// Copyright 2025 samber.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/samber/ro/blob/main/licenses/LICENSE.apache.md
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rohyperloglog
import (
// bearer:disable go_gosec_blocklist_md5
"crypto/md5"
// bearer:disable go_gosec_blocklist_sha1
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash/adler32"
"hash/crc32"
"hash/crc64"
"hash/fnv"
"hash/maphash"
)
// Convenience variables for easy access
var (
// StringHash provides hash functions for strings
StringHash = StringHashers{}
// BytesHash provides hash functions for byte slices
BytesHash = BytesHashers{}
)
// StringHashers provides common hash functions for strings
type StringHashers struct{}
// FNV64a returns a hash function using FNV-1a 64-bit
func (StringHashers) FNV64a() func(string) uint64 {
return func(input string) uint64 {
h := fnv.New64a()
h.Write([]byte(input))
return h.Sum64()
}
}
// FNV64 returns a hash function using FNV-1 64-bit
func (StringHashers) FNV64() func(string) uint64 {
return func(input string) uint64 {
h := fnv.New64()
h.Write([]byte(input))
return h.Sum64()
}
}
// FNV32a returns a hash function using FNV-1a 32-bit
func (StringHashers) FNV32a() func(string) uint64 {
return func(input string) uint64 {
h := fnv.New32a()
h.Write([]byte(input))
return uint64(h.Sum32())
}
}
// FNV32 returns a hash function using FNV-1 32-bit
func (StringHashers) FNV32() func(string) uint64 {
return func(input string) uint64 {
h := fnv.New32()
h.Write([]byte(input))
return uint64(h.Sum32())
}
}
// SHA256 returns a hash function using SHA-256
func (StringHashers) SHA256() func(string) uint64 {
return func(input string) uint64 {
hash := sha256.Sum256([]byte(input))
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// SHA1 returns a hash function using SHA-1
func (StringHashers) SHA1() func(string) uint64 {
return func(input string) uint64 {
// bearer:disable go_lang_weak_hash_sha1, go_gosec_crypto_weak_crypto
hash := sha1.Sum([]byte(input))
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// SHA512 returns a hash function using SHA-512
func (StringHashers) SHA512() func(string) uint64 {
return func(input string) uint64 {
hash := sha512.Sum512([]byte(input))
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// MD5 returns a hash function using MD5
func (StringHashers) MD5() func(string) uint64 {
return func(input string) uint64 {
// bearer:disable go_lang_weak_hash_md5, go_gosec_crypto_weak_crypto
hash := md5.Sum([]byte(input))
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// MapHash returns a hash function using maphash
func (StringHashers) MapHash() func(string) uint64 {
return func(input string) uint64 {
var h maphash.Hash
h.WriteString(input)
return h.Sum64()
}
}
// CRC32 returns a hash function using CRC-32
func (StringHashers) CRC32() func(string) uint64 {
return func(input string) uint64 {
return uint64(crc32.ChecksumIEEE([]byte(input)))
}
}
// CRC64 returns a hash function using CRC-64
func (StringHashers) CRC64() func(string) uint64 {
return func(input string) uint64 {
return crc64.Checksum([]byte(input), crc64.MakeTable(crc64.ISO))
}
}
// Adler32 returns a hash function using Adler-32
func (StringHashers) Adler32() func(string) uint64 {
return func(input string) uint64 {
return uint64(adler32.Checksum([]byte(input)))
}
}
// Jenkins returns a hash function using Jenkins hash (one-at-a-time)
func (StringHashers) Jenkins() func(string) uint64 {
return func(input string) uint64 {
var hash uint64
for _, c := range input {
hash += uint64(c)
hash += (hash << 10)
hash ^= (hash >> 6)
}
hash += (hash << 3)
hash ^= (hash >> 11)
hash += (hash << 15)
return hash
}
}
// DJB2 returns a hash function using DJB2 algorithm
func (StringHashers) DJB2() func(string) uint64 {
return func(input string) uint64 {
var hash uint64 = 5381
for _, c := range input {
hash = ((hash << 5) + hash) + uint64(c) // hash * 33 + c
}
return hash
}
}
// SDBM returns a hash function using SDBM algorithm
func (StringHashers) SDBM() func(string) uint64 {
return func(input string) uint64 {
var hash uint64 = 0
for _, c := range input {
hash = uint64(c) + (hash << 6) + (hash << 16) - hash
}
return hash
}
}
// Loselose returns a hash function using the "lose lose" algorithm
func (StringHashers) Loselose() func(string) uint64 {
return func(input string) uint64 {
var hash uint64 = 0
for _, c := range input {
hash += uint64(c)
}
return hash
}
}
// BytesHashers provides common hash functions for byte slices
type BytesHashers struct{}
// FNV64a returns a hash function using FNV-1a 64-bit for byte slices
func (BytesHashers) FNV64a() func([]byte) uint64 {
return func(input []byte) uint64 {
h := fnv.New64a()
h.Write(input)
return h.Sum64()
}
}
// FNV64 returns a hash function using FNV-1 64-bit for byte slices
func (BytesHashers) FNV64() func([]byte) uint64 {
return func(input []byte) uint64 {
h := fnv.New64()
h.Write(input)
return h.Sum64()
}
}
// FNV32a returns a hash function using FNV-1a 32-bit for byte slices
func (BytesHashers) FNV32a() func([]byte) uint64 {
return func(input []byte) uint64 {
h := fnv.New32a()
h.Write(input)
return uint64(h.Sum32())
}
}
// FNV32 returns a hash function using FNV-1 32-bit for byte slices
func (BytesHashers) FNV32() func([]byte) uint64 {
return func(input []byte) uint64 {
h := fnv.New32()
h.Write(input)
return uint64(h.Sum32())
}
}
// SHA256 returns a hash function using SHA-256 for byte slices
func (BytesHashers) SHA256() func([]byte) uint64 {
return func(input []byte) uint64 {
hash := sha256.Sum256(input)
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// SHA1 returns a hash function using SHA-1 for byte slices
func (BytesHashers) SHA1() func([]byte) uint64 {
return func(input []byte) uint64 {
// bearer:disable go_lang_weak_hash_sha1, go_gosec_crypto_weak_crypto
hash := sha1.Sum(input)
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// SHA512 returns a hash function using SHA-512 for byte slices
func (BytesHashers) SHA512() func([]byte) uint64 {
return func(input []byte) uint64 {
hash := sha512.Sum512(input)
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// MD5 returns a hash function using MD5 for byte slices
func (BytesHashers) MD5() func([]byte) uint64 {
return func(input []byte) uint64 {
// bearer:disable go_lang_weak_hash_md5, go_gosec_crypto_weak_crypto
hash := md5.Sum(input)
return uint64(hash[0])<<56 | uint64(hash[1])<<48 | uint64(hash[2])<<40 | uint64(hash[3])<<32 |
uint64(hash[4])<<24 | uint64(hash[5])<<16 | uint64(hash[6])<<8 | uint64(hash[7])
}
}
// MapHash returns a hash function using maphash for byte slices
func (BytesHashers) MapHash() func([]byte) uint64 {
return func(input []byte) uint64 {
var h maphash.Hash
h.Write(input)
return h.Sum64()
}
}
// CRC32 returns a hash function using CRC-32 for byte slices
func (BytesHashers) CRC32() func([]byte) uint64 {
return func(input []byte) uint64 {
return uint64(crc32.ChecksumIEEE(input))
}
}
// CRC64 returns a hash function using CRC-64 for byte slices
func (BytesHashers) CRC64() func([]byte) uint64 {
return func(input []byte) uint64 {
return crc64.Checksum(input, crc64.MakeTable(crc64.ISO))
}
}
// Adler32 returns a hash function using Adler-32 for byte slices
func (BytesHashers) Adler32() func([]byte) uint64 {
return func(input []byte) uint64 {
return uint64(adler32.Checksum(input))
}
}
// Jenkins returns a hash function using Jenkins hash (one-at-a-time) for byte slices
func (BytesHashers) Jenkins() func([]byte) uint64 {
return func(input []byte) uint64 {
var hash uint64
for _, c := range input {
hash += uint64(c)
hash += (hash << 10)
hash ^= (hash >> 6)
}
hash += (hash << 3)
hash ^= (hash >> 11)
hash += (hash << 15)
return hash
}
}
// DJB2 returns a hash function using DJB2 algorithm for byte slices
func (BytesHashers) DJB2() func([]byte) uint64 {
return func(input []byte) uint64 {
var hash uint64 = 5381
for _, c := range input {
hash = ((hash << 5) + hash) + uint64(c) // hash * 33 + c
}
return hash
}
}
// SDBM returns a hash function using SDBM algorithm for byte slices
func (BytesHashers) SDBM() func([]byte) uint64 {
return func(input []byte) uint64 {
var hash uint64 = 0
for _, c := range input {
hash = uint64(c) + (hash << 6) + (hash << 16) - hash
}
return hash
}
}
// Loselose returns a hash function using the "lose lose" algorithm for byte slices
func (BytesHashers) Loselose() func([]byte) uint64 {
return func(input []byte) uint64 {
var hash uint64 = 0
for _, c := range input {
hash += uint64(c)
}
return hash
}
}