-
Notifications
You must be signed in to change notification settings - Fork 22
/
use.go
477 lines (344 loc) · 8.21 KB
/
use.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package crypto
// Aes
func (this Cryptobin) Aes() Cryptobin {
this.multiple = Aes
return this
}
// Des
func (this Cryptobin) Des() Cryptobin {
this.multiple = Des
return this
}
// TwoDes
func (this Cryptobin) TwoDes() Cryptobin {
this.multiple = TwoDes
return this
}
// TripleDes
func (this Cryptobin) TripleDes() Cryptobin {
this.multiple = TripleDes
return this
}
// Twofish
func (this Cryptobin) Twofish() Cryptobin {
this.multiple = Twofish
return this
}
// Blowfish
func (this Cryptobin) Blowfish(salt ...string) Cryptobin {
this.multiple = Blowfish
if len(salt) > 0 {
this.config.Set("salt", []byte(salt[0]))
}
return this
}
// Tea
func (this Cryptobin) Tea(rounds ...int) Cryptobin {
this.multiple = Tea
if len(rounds) > 0 {
this.config.Set("rounds", rounds[0])
}
return this
}
// Xtea
func (this Cryptobin) Xtea() Cryptobin {
this.multiple = Xtea
return this
}
// Cast5
func (this Cryptobin) Cast5() Cryptobin {
this.multiple = Cast5
return this
}
// Idea
func (this Cryptobin) Idea() Cryptobin {
this.multiple = Idea
return this
}
// SM4
func (this Cryptobin) SM4() Cryptobin {
this.multiple = SM4
return this
}
// Chacha20 | Chacha20IETF | XChacha20
func (this Cryptobin) Chacha20(nonce string, counter ...uint32) Cryptobin {
this.multiple = Chacha20
this.config.Set("nonce", []byte(nonce))
if len(counter) > 0 {
this.config.Set("counter", counter[0])
}
return this
}
// Chacha20poly1305
// nonce is 12 bytes
func (this Cryptobin) Chacha20poly1305(nonce string, additional string) Cryptobin {
this.multiple = Chacha20poly1305
this.config.Set("nonce", []byte(nonce))
this.config.Set("additional", []byte(additional))
return this
}
// Chacha20poly1305X
// nonce is 24 bytes
func (this Cryptobin) Chacha20poly1305X(nonce string, additional string) Cryptobin {
this.multiple = Chacha20poly1305X
this.config.Set("nonce", []byte(nonce))
this.config.Set("additional", []byte(additional))
return this
}
// RC2
func (this Cryptobin) RC2() Cryptobin {
this.multiple = RC2
return this
}
// RC4
func (this Cryptobin) RC4() Cryptobin {
this.multiple = RC4
return this
}
// RC4MD5
func (this Cryptobin) RC4MD5() Cryptobin {
this.multiple = RC4MD5
return this
}
// RC5
func (this Cryptobin) RC5(wordSize, rounds uint) Cryptobin {
this.multiple = RC5
this.config.Set("word_size", wordSize)
this.config.Set("rounds", rounds)
return this
}
// Xts
// cipher 可用 [ Aes | Des | TripleDes | Tea | Xtea | Twofish | Blowfish | Cast5 | SM4]
func (this Cryptobin) Xts(cipher string, sectorNum uint64) Cryptobin {
this.multiple = Xts
this.config.Set("cipher", cipher)
this.config.Set("sector_num", sectorNum)
return this
}
// Salsa20
// key is 32 bytes, nonce is 16 bytes.
func (this Cryptobin) Salsa20(nonce string) Cryptobin {
this.multiple = Salsa20
this.config.Set("nonce", []byte(nonce))
return this
}
// Seed
// The key argument should be 16 bytes.
func (this Cryptobin) Seed() Cryptobin {
this.multiple = Seed
return this
}
// Aria
// key is 16, 24, or 32 bytes.
func (this Cryptobin) Aria() Cryptobin {
this.multiple = Aria
return this
}
// Camellia
// The key argument should be 16, 24, or 32 bytes.
func (this Cryptobin) Camellia() Cryptobin {
this.multiple = Camellia
return this
}
// Gost
// The key argument should be 32 bytes.
// sbox is [DESDerivedSbox | TestSbox | CryptoProSbox | SboxIdtc26gost28147paramZ]
// or set [][]byte data
func (this Cryptobin) Gost(sbox any) Cryptobin {
this.multiple = Gost
this.config.Set("sbox", sbox)
return this
}
// Kuznyechik
// The key argument should be 32 bytes.
func (this Cryptobin) Kuznyechik() Cryptobin {
this.multiple = Kuznyechik
return this
}
// Skipjack
// The key argument should be 10 bytes.
func (this Cryptobin) Skipjack() Cryptobin {
this.multiple = Skipjack
return this
}
// Serpent
// The key argument should be 16, 24, 32 bytes.
func (this Cryptobin) Serpent() Cryptobin {
this.multiple = Serpent
return this
}
// 使用类型
func (this Cryptobin) MultipleBy(multiple Multiple, cfg ...map[string]any) Cryptobin {
this.multiple = multiple
for _, v := range cfg {
for kk, vv := range v {
this.config.Set(kk, vv)
}
}
return this
}
// ==========
// 电码本模式
func (this Cryptobin) ECB() Cryptobin {
this.mode = ECB
return this
}
// 密码分组链接模式
func (this Cryptobin) CBC() Cryptobin {
this.mode = CBC
return this
}
// 填充密码块链接模式
func (this Cryptobin) PCBC() Cryptobin {
this.mode = PCBC
return this
}
// 密码反馈模式
func (this Cryptobin) CFB() Cryptobin {
this.mode = CFB
return this
}
// 密码反馈模式
func (this Cryptobin) CFB1() Cryptobin {
this.mode = CFB1
return this
}
// 密码反馈模式, 8字节
func (this Cryptobin) CFB8() Cryptobin {
this.mode = CFB8
return this
}
// 密码反馈模式
func (this Cryptobin) CFB16() Cryptobin {
this.mode = CFB16
return this
}
// 密码反馈模式
func (this Cryptobin) CFB32() Cryptobin {
this.mode = CFB32
return this
}
// 密码反馈模式
func (this Cryptobin) CFB64() Cryptobin {
this.mode = CFB64
return this
}
// 密码反馈模式, 标准库 CFB 别名
func (this Cryptobin) CFB128() Cryptobin {
this.mode = CFB128
return this
}
// 输出反馈模式
func (this Cryptobin) OFB() Cryptobin {
this.mode = OFB
return this
}
// 输出反馈模式, 8字节
func (this Cryptobin) OFB8() Cryptobin {
this.mode = OFB8
return this
}
// 计算器模式
func (this Cryptobin) CTR() Cryptobin {
this.mode = CTR
return this
}
// GCM
func (this Cryptobin) GCM(nonce string, additional ...string) Cryptobin {
this.mode = GCM
this.config.Set("nonce", []byte(nonce))
if len(additional) > 0 {
this.config.Set("additional", []byte(additional[0]))
}
return this
}
// CCM
// ccm nounce size, should be in [7,13]
func (this Cryptobin) CCM(nonce string, additional ...string) Cryptobin {
this.mode = CCM
this.config.Set("nonce", []byte(nonce))
if len(additional) > 0 {
this.config.Set("additional", []byte(additional[0]))
}
return this
}
// 使用模式
func (this Cryptobin) ModeBy(mode Mode, cfg ...map[string]any) Cryptobin {
this.mode = mode
for _, v := range cfg {
for kk, vv := range v {
this.config.Set(kk, vv)
}
}
return this
}
// ==========
// 不补码
func (this Cryptobin) NoPadding() Cryptobin {
this.padding = NoPadding
return this
}
// Zero 补码
func (this Cryptobin) ZeroPadding() Cryptobin {
this.padding = ZeroPadding
return this
}
// PKCS5 补码
func (this Cryptobin) PKCS5Padding() Cryptobin {
this.padding = PKCS5Padding
return this
}
// PKCS7 补码
func (this Cryptobin) PKCS7Padding() Cryptobin {
this.padding = PKCS7Padding
return this
}
// X923 补码
func (this Cryptobin) X923Padding() Cryptobin {
this.padding = X923Padding
return this
}
// ISO10126 补码
func (this Cryptobin) ISO10126Padding() Cryptobin {
this.padding = ISO10126Padding
return this
}
// ISO7816_4 补码
func (this Cryptobin) ISO7816_4Padding() Cryptobin {
this.padding = ISO7816_4Padding
return this
}
// ISO97971 补码
func (this Cryptobin) ISO97971Padding() Cryptobin {
this.padding = ISO97971Padding
return this
}
// TBC 补码
func (this Cryptobin) TBCPadding() Cryptobin {
this.padding = TBCPadding
return this
}
// PKCS1 补码
func (this Cryptobin) PKCS1Padding(bt ...string) Cryptobin {
this.padding = PKCS1Padding
if len(bt) > 0 {
this.config.Set("pkcs1_padding_bt", bt[0])
}
return this
}
// 使用补码算法
func (this Cryptobin) PaddingBy(padding Padding, cfg ...map[string]any) Cryptobin {
this.padding = padding
for _, v := range cfg {
for kk, vv := range v {
this.config.Set(kk, vv)
}
}
return this
}
// ==========
// 不做处理
func (this Cryptobin) NoParse() Cryptobin {
this.parsedData = this.data
return this
}