-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstringhashes.go
184 lines (158 loc) · 6.02 KB
/
stringhashes.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
// Package dgohash implements a number of well-known string hashing functions.
// They all conform to the hash.Hash32 interface.
// Copyright (c) 2011 Damian Gryski <[email protected]>
// Licensed under the GPLv3, or at your option any later version.
package dgohash
import (
"hash"
)
type javaStringHash32 uint32
// NewJava32 returns a new hash.Hash32 object, computing Java's string.hashCode() algorithm
func NewJava32() hash.Hash32 { sh := javaStringHash32(0); sh.Reset(); return &sh }
func (sh *javaStringHash32) Size() int { return 4 }
func (sh *javaStringHash32) BlockSize() int { return 1 }
func (sh *javaStringHash32) Sum32() uint32 { return uint32(*sh) }
func (sh *javaStringHash32) Reset() { *sh = javaStringHash32(0) }
func (sh *javaStringHash32) Sum(b []byte) []byte {
v := uint32(*sh)
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (sh *javaStringHash32) Write(b []byte) (int, error) {
h := uint32(*sh)
for _, c := range b {
h = 31*h + uint32(c)
}
*sh = javaStringHash32(h)
return len(b), nil
}
type djb2StringHash32 uint32
// NewDjb32 returns a new hash.Hash32 object, computing Daniel J. Bernstein's hash
func NewDjb32() hash.Hash32 { sh := djb2StringHash32(0); sh.Reset(); return &sh }
func (sh *djb2StringHash32) Size() int { return 4 }
func (sh *djb2StringHash32) BlockSize() int { return 1 }
func (sh *djb2StringHash32) Sum32() uint32 { return uint32(*sh) }
func (sh *djb2StringHash32) Reset() { *sh = djb2StringHash32(5381) }
func (sh *djb2StringHash32) Sum(b []byte) []byte {
v := uint32(*sh)
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (sh *djb2StringHash32) Write(b []byte) (int, error) {
h := uint32(*sh)
for _, c := range b {
h = 33*h + uint32(c)
}
*sh = djb2StringHash32(h)
return len(b), nil
}
type djb2aStringHash32 uint32
// NewDjb32a returns a new hash.Hash32 object, computing a variant of Daniel J. Bernstein's hash that uses xor instead of +
func NewDjb32a() hash.Hash32 { sh := djb2aStringHash32(0); sh.Reset(); return &sh }
func (sh *djb2aStringHash32) Size() int { return 4 }
func (sh *djb2aStringHash32) BlockSize() int { return 1 }
func (sh *djb2aStringHash32) Sum32() uint32 { return uint32(*sh) }
func (sh *djb2aStringHash32) Reset() { *sh = djb2aStringHash32(5381) }
func (sh *djb2aStringHash32) Sum(b []byte) []byte {
v := uint32(*sh)
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (sh *djb2aStringHash32) Write(b []byte) (int, error) {
h := uint32(*sh)
for _, c := range b {
h = 33*h ^ uint32(c)
}
*sh = djb2aStringHash32(h)
return len(b), nil
}
type elf32StringHash32 uint32
// NewElf32 returns a new hash.Hash32 object computing the ELF32 symbol hash
func NewElf32() hash.Hash32 { sh := elf32StringHash32(0); sh.Reset(); return &sh }
func (sh *elf32StringHash32) Size() int { return 4 }
func (sh *elf32StringHash32) BlockSize() int { return 1 }
func (sh *elf32StringHash32) Sum32() uint32 { return uint32(*sh) }
func (sh *elf32StringHash32) Reset() { *sh = elf32StringHash32(0) }
func (sh *elf32StringHash32) Sum(b []byte) []byte {
v := uint32(*sh)
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (sh *elf32StringHash32) Write(b []byte) (int, error) {
h := uint32(*sh)
for _, c := range b {
h = (h << 4) + uint32(c)
g := h & 0xf0000000
if g != 0 {
h ^= g >> 24
h &= ^g
}
}
*sh = elf32StringHash32(h)
return len(b), nil
}
type sdbmStringHash32 uint32
// NewSDBM32 returns a new hash.Hash32 object, computing the string hash function from SDBM
func NewSDBM32() hash.Hash32 { sh := sdbmStringHash32(0); sh.Reset(); return &sh }
func (sh *sdbmStringHash32) Size() int { return 4 }
func (sh *sdbmStringHash32) BlockSize() int { return 1 }
func (sh *sdbmStringHash32) Sum32() uint32 { return uint32(*sh) }
func (sh *sdbmStringHash32) Reset() { *sh = sdbmStringHash32(0) }
func (sh *sdbmStringHash32) Sum(b []byte) []byte {
v := uint32(*sh)
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (sh *sdbmStringHash32) Write(b []byte) (int, error) {
h := uint32(*sh)
for _, c := range b {
h = uint32(c) + (h << 6) + (h << 16) - h
}
*sh = sdbmStringHash32(h)
return len(b), nil
}
type sqlite3StringHash32 uint32
// NewSQLite32 returns a new hash.Hash32 object, computing the string hash function from SQLite3
func NewSQLite32() hash.Hash32 { sh := sqlite3StringHash32(0); sh.Reset(); return &sh }
func (sh *sqlite3StringHash32) Size() int { return 4 }
func (sh *sqlite3StringHash32) BlockSize() int { return 1 }
func (sh *sqlite3StringHash32) Sum32() uint32 { return uint32(*sh) }
func (sh *sqlite3StringHash32) Reset() { *sh = sqlite3StringHash32(0) }
func (sh *sqlite3StringHash32) Sum(b []byte) []byte {
v := uint32(*sh)
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}
func (sh *sqlite3StringHash32) Write(b []byte) (int, error) {
h := uint32(*sh)
for _, c := range b {
h = (h << 3) ^ h ^ uint32(c)
}
*sh = sqlite3StringHash32(h)
return len(b), nil
}
type jenkinsStringHash32 uint32
// NewJenkins32 returns a new hash.Hash32 object, computing the Robert Jenkins' one-at-a-time string hash function
func NewJenkins32() hash.Hash32 {
var s = jenkinsStringHash32(0)
return &s
}
func (sh *jenkinsStringHash32) Size() int { return 4 }
func (sh *jenkinsStringHash32) BlockSize() int { return 1 }
func (sh *jenkinsStringHash32) Reset() { *sh = jenkinsStringHash32(0) }
func (sh *jenkinsStringHash32) Write(b []byte) (int, error) {
h := uint32(*sh)
for _, c := range b {
h += uint32(c)
h += (h << 10)
h ^= (h >> 6)
}
*sh = jenkinsStringHash32(h)
return len(b), nil
}
func (sh *jenkinsStringHash32) Sum32() uint32 {
h := uint32(*sh) // copy so we don't mess with the internal state
// Jenkins' finalize
h += (h << 3)
h ^= (h >> 11)
h += (h << 15)
return h
}
func (sh *jenkinsStringHash32) Sum(b []byte) []byte {
v := sh.Sum32()
return append(b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v))
}