-
Notifications
You must be signed in to change notification settings - Fork 22
/
helper.go
84 lines (69 loc) · 1.89 KB
/
helper.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
package pkcs8
// hash 列表
var HashMap = map[string]Hash{
"MD5": MD5,
"SHA1": SHA1,
"SHA224": SHA224,
"SHA256": SHA256,
"SHA384": SHA384,
"SHA512": SHA512,
"SHA512_224": SHA512_224,
"SHA512_256": SHA512_256,
"SM3": SM3,
}
// 获取 hash 类型
func GetHashFromName(name string) Hash {
if data, ok := HashMap[name]; ok {
return data
}
return HashMap["SHA1"]
}
// 解析配置
func ParseOpts(opts ...any) (Opts, error) {
if len(opts) == 0 {
return DefaultOpts, nil
}
switch newOpt := opts[0].(type) {
case Opts:
return newOpt, nil
case Cipher:
kdfOpts := PBKDF2Opts{
SaltSize: 16,
IterationCount: 10000,
}
cipher := newOpt
// 设置
newOpts := Opts{
Cipher: cipher,
KDFOpts: kdfOpts,
}
return newOpts, nil
case string:
// DESCBC | DESEDE3CBC
// AES128CBC | AES192CBC | AES256CBC
// AES128GCM | AES192GCM | AES256GCM
// SM4CBC | SM4GCM
opt := "AES256CBC"
if len(opts) > 0 {
opt = opts[0].(string)
}
kdfOpts := PBKDF2Opts{
SaltSize: 16,
IterationCount: 10000,
}
// MD5 | SHA1 | SHA224 | SHA256 | SHA384
// SHA512 | SHA512_224 | SHA512_256 | SM3
if len(opts) > 1 {
hash := opts[1].(string)
kdfOpts.HMACHash = GetHashFromName(hash)
}
cipher := GetCipherFromName(opt)
// 设置
newOpts := Opts{
Cipher: cipher,
KDFOpts: kdfOpts,
}
return newOpts, nil
}
return DefaultOpts, nil
}