-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmixer.go
338 lines (294 loc) · 8.82 KB
/
mixer.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
package mixer
import (
"encoding/base32"
"fmt"
"strconv"
"sync"
)
const (
//DefaultSalt salt for random seed: 202002022002
DefaultSalt = "202002022002" //2020.02.02 20:02
//DefaultPaddingLength default padding length
DefaultPaddingLength = 16
)
var (
//StdMixer std mixer is alias AlphanumericCaseMixer
StdMixer = AlphanumericCaseMixer
//AlphanumericCaseMixer the alphanumeric include upper and lower:`0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
AlphanumericCaseMixer = NewWith(DefaultSalt, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
//AlphanumericUpperMixer the alphanumeric include upper:`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ`
AlphanumericUpperMixer = NewWith(DefaultSalt, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
//AlphanumericLowerMixer the alphanumeric include lower:`0123456789abcdefghijklmnopqrstuvwxyz`
AlphanumericLowerMixer = NewWith(DefaultSalt, "0123456789abcdefghijklmnopqrstuvwxyz")
//AlphabetCaseMixer the upper alphabet:`abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
AlphabetCaseMixer = NewWith(DefaultSalt, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
//AlphabetUpperMixer the upper alphabet:`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
AlphabetUpperMixer = NewWith(DefaultSalt, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
//AlphabetLowerMixer the lower alphabet:`abcdefghijklmnopqrstuvwxyz`
AlphabetLowerMixer = NewWith(DefaultSalt, "abcdefghijklmnopqrstuvwxyz")
//HexCaseMixer the hex alphabet and numeric:`0123456789abcdefABCDEF`
HexCaseMixer = NewWith(DefaultSalt, "0123456789abcdefABCDEF")
//HexUpperMixer the hex alphabet and numeric:`0123456789abcdef`
HexUpperMixer = NewWith(DefaultSalt, "0123456789ABCDEF")
//HexLowerMixer the hex alphabet and numeric:`0123456789abcdef`
HexLowerMixer = NewWith(DefaultSalt, "0123456789abcdef")
//NumericMixer the numeric:`0123456789abcdef`
NumericMixer = NewWith(DefaultSalt, "0123456789")
//SymbolsMixer the symbols chars
SymbolsMixer = NewWith(DefaultSalt, "0123456789ABCabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+-=.")
)
var alphabetsRunes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
var base32NoPadding = base32.StdEncoding.WithPadding(base32.NoPadding)
//Config configuration for new mixer
type Config struct {
Salt string //salt for random seed
MixChars string //chars for mix
}
//Mixer a mixer instance for encode/decode
type Mixer struct {
config Config
cacheSaltSeeds sync.Map //cache salt seed
mapEncodeChars map[rune]rune
mapDecodeChars map[rune]rune
}
//NewWithConfig create a new mixer
func NewWithConfig(cfg Config) (*Mixer, error) {
if cfg.Salt == "" {
return nil, fmt.Errorf("salt is not allow empty")
}
seed := sumSaltSeed(cfg.Salt)
var cacheSaltSeeds sync.Map
cacheSaltSeeds.Store(cfg.Salt, seed)
mapEncodeTable := createMapChars(cfg.MixChars, seed)
if len(mapEncodeTable) < 2 {
return nil, fmt.Errorf("mixChars `%v` is not invalid", cfg.MixChars)
}
mapDecodeTable := make(map[rune]rune, 0)
for k, v := range mapEncodeTable {
mapDecodeTable[v] = k
}
return &Mixer{
config: cfg,
cacheSaltSeeds: cacheSaltSeeds,
mapEncodeChars: mapEncodeTable,
mapDecodeChars: mapDecodeTable,
}, nil
}
//New create a new mixer with case sensitive alphanumeric
func New() *Mixer {
return StdMixer
}
//NewWith create a new mixer with args
func NewWith(salt string, mixChars string) *Mixer {
mixer, err := NewWithConfig(Config{
Salt: salt,
MixChars: mixChars,
})
if err != nil {
panic(err)
}
return mixer
}
//WithSalt create copy Mixer with new salt
func (m *Mixer) WithSalt(salt string) *Mixer {
cfg := m.config
cfg.Salt = salt
mix, err := NewWithConfig(cfg)
if err != nil {
panic(err)
}
return mix
}
//Encode encode char array
func (m Mixer) Encode(password string, data []rune) []rune {
seed := m.getSeed(password)
outChars := make([]rune, len(data))
for i, c := range data {
if v, ok := m.mapEncodeChars[c]; ok {
outChars[i] = v
} else {
outChars[i] = c
}
}
return randomEncode(outChars, seed)
}
//Decode decode char array
func (m Mixer) Decode(password string, data []rune) []rune {
seed := m.getSeed(password)
outChars := randomDecode(data, seed)
for i, c := range outChars {
if rc, ok := m.mapDecodeChars[c]; ok {
outChars[i] = rc
} else {
outChars[i] = c
}
}
return outChars
}
//EncodeNumber encode int64 number
func (m Mixer) EncodeNumber(password string, value int64) string {
return m.EncodeNumberPadding(password, value, DefaultPaddingLength)
}
//EncodeNumberPadding encode int64 number
func (m Mixer) EncodeNumberPadding(password string, value int64, paddingLen int) string {
return m.EncodeBase32Padding(password, strconv.FormatInt(value, 10), paddingLen)
}
//DecodeNumber decode int64 number
func (m Mixer) DecodeNumber(password string, data string) (int64, error) {
decodeString, err := m.DecodeBase32(password, data)
if err != nil {
return 0, err
}
val, err := strconv.ParseInt(decodeString, 10, 64)
if err != nil {
return 0, err
}
return val, nil
}
//EncodeID encode uint64 ID
func (m Mixer) EncodeID(password string, id uint64) string {
return m.EncodeIDPadding(password, id, DefaultPaddingLength)
}
//EncodeIDPadding encode uint64 ID
func (m Mixer) EncodeIDPadding(password string, id uint64, paddingLen int) string {
return m.EncodeBase32Padding(password, strconv.FormatUint(id, 10), paddingLen)
}
//DecodeID decode uint64 ID
func (m Mixer) DecodeID(password string, data string) (uint64, error) {
decodeString, err := m.DecodeBase32(password, data)
if err != nil {
return 0, err
}
val, err := strconv.ParseUint(decodeString, 10, 64)
if err != nil {
return 0, err
}
return val, nil
}
// DecodeBase32 decode base32 data
func (m Mixer) DecodeBase32(password string, data string) (string, error) {
decodeRunes := m.Decode(password, []rune(data))
baseRunes := make([]rune, 0)
for _, r := range decodeRunes {
if r != '0' {
baseRunes = append(baseRunes, r)
} else {
break
}
}
value, err := base32NoPadding.DecodeString(string(baseRunes))
if err != nil {
return "", err
}
return string(value), nil
}
// EncodeBase32 encode base32 data
func (m Mixer) EncodeBase32(password string, value string) string {
return m.EncodeBase32Padding(password, value, 0)
}
// EncodeBase32Padding encode base32 data
func (m Mixer) EncodeBase32Padding(password string, value string, paddingLen int) string {
//base32 chars range: [A-Z] [2-7]
runes := []rune(base32NoPadding.EncodeToString([]byte(value)))
numLen := len(runes)
if paddingLen > 0 && numLen < paddingLen {
seed := m.getSeed(password)
pdSize := paddingLen - numLen - 1
runes = append(runes, '0') //zero to split padding chars
if pdSize > 0 {
runes = append(runes, randomAlphabets(pdSize, seed)...)
}
}
return string(m.Encode(password, runes))
}
//EncodeString encode string
func (m Mixer) EncodeString(password, data string) string {
return string(m.Encode(password, []rune(data)))
}
//DecodeString decode string
func (m Mixer) DecodeString(password, data string) string {
return string(m.Decode(password, []rune(data)))
}
//Config return current Config
func (m Mixer) Config() Config {
return m.config
}
func (m Mixer) getSeed(password string) int64 {
if password == "" {
password = m.config.Salt
}
seed, ok := m.cacheSaltSeeds.Load(password)
if ok {
return seed.(int64)
}
saltSeed := sumSaltSeed(password)
m.cacheSaltSeeds.Store(password, saltSeed)
return saltSeed
}
func uniqueChars(chars string) []rune {
// Use map to record duplicates as we find them.
mapCheck := make(map[rune]bool)
list := make([]rune, 0)
for _, v := range []rune(chars) {
if mapCheck[v] == true {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
mapCheck[v] = true
// Append to list slice.
list = append(list, v)
}
}
// Return the new slice.
return list
}
func createMapChars(chars string, seed int64) map[rune]rune {
dictChars := uniqueChars(chars)
rnChars := randomEncode(dictChars, seed)
dictMaps := make(map[rune]rune, 0)
for i := 0; i < len(dictChars); i++ {
key := dictChars[i]
val := rnChars[i]
if _, ok := dictMaps[key]; ok {
continue
}
dictMaps[key] = val
}
return dictMaps
}
func randomEncode(chars []rune, seed int64) []rune {
src := chars
final := make([]rune, len(src))
rn := NewLGC(seed)
perm := rn.Perm(len(src))
for i, v := range perm {
final[v] = src[i]
}
return final
}
func randomDecode(chars []rune, seed int64) []rune {
src := chars
final := make([]rune, len(src))
rn := NewLGC(seed)
perm := rn.Perm(len(src))
for i, v := range perm {
final[i] = src[v]
}
return final
}
func sumSaltSeed(str string) int64 {
var sum int64
for _, v := range []rune(str) {
sum += int64(v)
}
return sum
}
func randomAlphabets(n int, seed int64) []rune {
chars := make([]rune, n)
size := len(alphabetsRunes)
rn := NewLGC(seed)
for i := 0; i < n; i++ {
chars[i] = alphabetsRunes[rn.Intn(size)]
}
return chars
}