-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoption.go
82 lines (70 loc) · 1.78 KB
/
hoption.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
package password
import (
"strings"
)
const saltEntropy = 64
const (
md5Algo = "md5"
unsaltedMd5Algo = "unsalted_md5"
pbkdf2Sha256Algo = "pbkdf2_sha256"
pbkdf2Sha1Algo = "pbkdf2_sha1"
argon2Algo = "argon2id"
bcryptAlgo = "bcrypt"
bcryptSha256Algo = "bcrypt_sha256"
scryptAlgo = "scrypt"
sha1Algo = "sha1"
)
var supportAlgorithms = map[string]struct{}{
md5Algo: {},
unsaltedMd5Algo: {},
pbkdf2Sha256Algo: {},
pbkdf2Sha1Algo: {},
argon2Algo: {},
bcryptAlgo: {},
bcryptSha256Algo: {},
scryptAlgo: {},
sha1Algo: {},
}
// HasherOption Hasher option
type HasherOption struct {
// Algorithm: Support md5, unsalted_md5, pbkdf2_sha256, pbkdf2_sha1,
// argon2id, bcrypt, bcrypt_sha256, scrypt, sha1
Algorithm string `json:"algorithm"`
Secret string `json:"secret"`
// Salt: cannot contain '$'
Salt string `json:"salt"`
// Iterations: should be gratter than 0
Iterations int `json:"iterations"`
Params interface{} `json:"params"`
}
func (ho *HasherOption) validate() error {
if _, ok := supportAlgorithms[ho.Algorithm]; !ok {
return errUnknownAlgorithm
}
if strings.Contains(ho.Salt, sep) {
return errIllegalSalt
}
if ho.Iterations <= 0 {
return errIllegalIterations
}
return nil
}
func (ho *HasherOption) NewHasher() (Hasher, error) {
var hasher Hasher
var err error
switch ho.Algorithm {
case unsaltedMd5Algo, md5Algo:
hasher, err = newMD5Hasher(ho)
case pbkdf2Sha1Algo, pbkdf2Sha256Algo:
hasher, err = newPBKDDF2Hasher(ho)
case argon2Algo:
hasher, err = newArgon2Hasher(ho)
case bcryptSha256Algo, bcryptAlgo:
hasher, err = newBcryptHasher(ho)
case scryptAlgo:
hasher, err = newScryptHasher(ho)
case sha1Algo:
hasher, err = newSha1Hasher(ho)
}
return hasher, err
}