-
Notifications
You must be signed in to change notification settings - Fork 0
/
argon2.go
147 lines (129 loc) · 3 KB
/
argon2.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
package password
import (
"encoding/hex"
"strconv"
"strings"
"golang.org/x/crypto/argon2"
)
// Argon2Params Argon2id parameters
type Argon2Params struct {
memory uint32
iterations uint32
parallelism uint8
saltLength int
keyLength uint32
}
var defaultArgon2Params = &Argon2Params{
memory: 64 * 1024,
iterations: 1,
parallelism: 4,
saltLength: 16,
keyLength: 32,
}
type argon2Hasher struct {
params *Argon2Params
}
func (hasher *argon2Hasher) Encode(password string) (string, error) {
salt, err := generateRandomBytes(hasher.params.saltLength)
if err != nil {
return "", err
}
return hasher.encode(password, salt, hasher.params)
}
func (hasher *argon2Hasher) encode(password string, salt []byte, params *Argon2Params) (string, error) {
hash := argon2.IDKey(
[]byte(password),
salt,
params.iterations,
params.memory,
params.parallelism,
params.keyLength)
p := []string{
argon2Algo,
hex.EncodeToString(salt),
strconv.Itoa(int(params.iterations)),
strconv.Itoa(int(params.memory)),
strconv.Itoa(int(params.parallelism)),
strconv.Itoa(int(params.keyLength)),
hex.EncodeToString(hash),
}
return strings.Join(p, sep), nil
}
func (hasher *argon2Hasher) Decode(encoded string) (*PasswordInfo, error) {
parts := strings.SplitN(encoded, sep, 7)
if parts[0] != argon2Algo {
return nil, errUnknownAlgorithm
}
iter, err := strconv.Atoi(parts[2])
if err != nil {
return nil, err
}
memory, err := strconv.Atoi(parts[3])
if err != nil {
return nil, err
}
parallelism, err := strconv.Atoi(parts[4])
if err != nil {
return nil, err
}
keyLength, err := strconv.Atoi(parts[5])
if err != nil {
return nil, err
}
salt, err := hex.DecodeString(parts[1])
if err != nil {
return nil, err
}
return &PasswordInfo{
Algorithm: parts[0],
Hash: parts[6],
Salt: parts[1],
Iterations: iter,
Others: &Argon2Params{
memory: uint32(memory),
iterations: uint32(iter),
parallelism: uint8(parallelism),
saltLength: len(salt),
keyLength: uint32(keyLength),
},
}, nil
}
func (hasher *argon2Hasher) Verify(password, encoded string) bool {
pi, err := hasher.Decode(encoded)
if err != nil {
return false
}
params := pi.Others.(*Argon2Params)
salt, _ := hex.DecodeString(pi.Salt)
encoded2, err := hasher.encode(password, salt, params)
if err != nil {
return false
}
return encoded2 == encoded
}
func (hasher *argon2Hasher) MustUpdate(encoded string) bool {
pi, err := hasher.Decode(encoded)
if err != nil {
return false
}
p := pi.Others.(*Argon2Params)
return *p != *hasher.params
}
func (hasher *argon2Hasher) Harden(password, encoded string) (string, error) {
// TODO(zhaowentao): 处理 Verify
return encoded, nil
}
func newArgon2Hasher(opt *HasherOption) (Hasher, error) {
var params *Argon2Params
if opt.Params == nil {
params = defaultArgon2Params
} else {
p, ok := opt.Params.(*Argon2Params)
if !ok {
params = defaultArgon2Params
} else {
params = p
}
}
return &argon2Hasher{params: params}, nil
}