-
Notifications
You must be signed in to change notification settings - Fork 0
/
argon2_wrapper_test.go
245 lines (227 loc) · 6.19 KB
/
argon2_wrapper_test.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
package argon2
import (
"regexp"
"strings"
"testing"
)
func TestCreateHash(t *testing.T) {
hashRX, err := regexp.Compile(`^\$argon2i\$v=19\$m=65536,t=1,p=2\$[A-Za-z0-9+/]{22}\$[A-Za-z0-9+/]{43}$`)
if err != nil {
t.Fatal(err)
}
// without secret
hash1, err := CreateHash("pa$$word", "", DefaultParams)
if err != nil {
t.Fatal(err)
}
if !hashRX.MatchString(hash1) {
t.Errorf("hash %q not in correct format", hash1)
}
hash2, err := CreateHash("pa$$word", "", DefaultParams)
if err != nil {
t.Fatal(err)
}
if strings.Compare(hash1, hash2) == 0 {
t.Error("hashes must be unique")
}
// with secret
hash1WithSecret, err := CreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
if !hashRX.MatchString(hash1WithSecret) {
t.Errorf("hash %q not in correct format", hash1WithSecret)
}
hash2WithSecret, err := CreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
if strings.Compare(hash1WithSecret, hash2WithSecret) == 0 {
t.Error("hashes must be unique")
}
}
func TestIDCreateHash(t *testing.T) {
hashRX, err := regexp.Compile(`^\$argon2id\$v=19\$m=65536,t=1,p=2\$[A-Za-z0-9+/]{22}\$[A-Za-z0-9+/]{43}$`)
if err != nil {
t.Fatal(err)
}
// without secret
hash1, err := IDCreateHash("pa$$word", "", DefaultParams)
if err != nil {
t.Fatal(err)
}
if !hashRX.MatchString(hash1) {
t.Errorf("hash %q not in correct format", hash1)
}
hash2, err := IDCreateHash("pa$$word", "", DefaultParams)
if err != nil {
t.Fatal(err)
}
if strings.Compare(hash1, hash2) == 0 {
t.Error("hashes must be unique")
}
// with secret
hash1WithSecret, err := IDCreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
if !hashRX.MatchString(hash1WithSecret) {
t.Errorf("hash %q not in correct format", hash1WithSecret)
}
hash2WithSecret, err := IDCreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
if strings.Compare(hash1WithSecret, hash2WithSecret) == 0 {
t.Error("hashes must be unique")
}
}
func TestComparePasswordAndHash(t *testing.T) {
hash, err := CreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
match, err := ComparePasswordAndHash("pa$$word", "$€cr€t", hash)
if err != nil {
t.Fatal(err)
}
if !match {
t.Error("expected password and hash to match")
}
match, err = ComparePasswordAndHash("otherPa$$word", "$€cr€t", hash)
if err != nil {
t.Fatal(err)
}
if match {
t.Error("expected password and hash to not match")
}
hash, err = IDCreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
match, err = ComparePasswordAndHash("pa$$word", "$€cr€t", hash)
if err != nil {
t.Fatal(err)
}
if !match {
t.Error("expected password and hash to match")
}
match, err = ComparePasswordAndHash("otherPa$$word", "$€cr€t", hash)
if err != nil {
t.Fatal(err)
}
if match {
t.Error("expected password and hash to not match")
}
}
func TestDecodeHash(t *testing.T) {
// Argon2i
hash, err := CreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
argon2Variant, params, _, _, err := DecodeHash(hash)
if err != nil {
t.Fatal(err)
}
if *params != *DefaultParams {
t.Fatalf("expected %#v got %#v", *DefaultParams, *params)
}
if argon2Variant != argon2i {
t.Fatalf("argon2 variant: expected %v got %v", argon2i, argon2Variant)
}
// Argon2id
hash, err = IDCreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
argon2Variant, params, _, _, err = DecodeHash(hash)
if err != nil {
t.Fatal(err)
}
if *params != *DefaultParams {
t.Fatalf("expected %#v got %#v", *DefaultParams, *params)
}
if argon2Variant != argon2id {
t.Fatalf("argon2 variant: expected %v got %v", argon2id, argon2Variant)
}
}
func TestCheckHash(t *testing.T) {
// without secret
hash, err := CreateHash("pa$$word", "", DefaultParams)
if err != nil {
t.Fatal(err)
}
ok, params, err := CheckHash("pa$$word", "", hash)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("expected password to match")
}
if *params != *DefaultParams {
t.Fatalf("expected %#v got %#v", *DefaultParams, *params)
}
// with secret
hash, err = CreateHash("pa$$word", "$€cr€t", DefaultParams)
if err != nil {
t.Fatal(err)
}
ok, params, err = CheckHash("pa$$word", "$€cr€t", hash)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("expected password to match")
}
if *params != *DefaultParams {
t.Fatalf("expected %#v got %#v", *DefaultParams, *params)
}
}
func TestStrictDecoding(t *testing.T) {
// Argon2id without secret
// password: "bug"
// secret: ""
// valid hash: $argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tE
ok, _, err := CheckHash("bug", "", "$argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tE")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("expected password to match")
}
// changed one last character of the hash
ok, _, err = CheckHash("bug", "", "$argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tF")
if err == nil {
t.Fatal("Hash validation should fail")
}
if ok {
t.Fatal("Hash validation should fail")
}
// Argon2id with secret
// password: "bug"
// secret: "12€45"
// valid hash: $argon2id$v=19$m=65536,t=1,p=2$xXH1+P7o0rwI9/lXEcPWkg$HAHY7gZ9CgbAFRQmQLk7v7uDEgomp2CSO/rrEBAvfHg
ok, _, err = CheckHash("bug", "12€45", "$argon2id$v=19$m=65536,t=1,p=2$xXH1+P7o0rwI9/lXEcPWkg$HAHY7gZ9CgbAFRQmQLk7v7uDEgomp2CSO/rrEBAvfHg")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("expected password to match")
}
// changed one last character of the hash
ok, _, err = CheckHash("bug", "", "$argon2id$v=19$m=65536,t=1,p=2$xXH1+P7o0rwI9/lXEcPWkg$HAHY7gZ9CgbAFRQmQLk7v7uDEgomp2CSO/rrEBAvfHG")
if err == nil {
t.Fatal("Hash validation should fail")
}
if ok {
t.Fatal("Hash validation should fail")
}
}
func TestVariant(t *testing.T) {
// Hash contains wrong variant
_, _, err := CheckHash("pa$$word", "", "$argon2d$v=19$m=16,t=2,p=1$RDZuTU9Mam1TemlBaUVtNA$iDDBu2UH7maUgYcBWCgTVw")
if err != ErrIncompatibleVariant {
t.Fatalf("expected error %s", ErrIncompatibleVariant)
}
}