forked from alexedwards/argon2id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_test.go
209 lines (175 loc) · 6.73 KB
/
lib_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
package lambda_argon
import (
"errors"
"github.com/jgroeneveld/trial/assert"
"math/rand"
"regexp"
"testing"
"time"
)
const (
shortPassword = "pa$$word"
otherPassword = "otherPa$$word"
)
var longPassword = generateRandomString(int(lambdaParams.KeyLength) * 2)
var goodPassword = generateRandomString(MaxPasswordLength)
func TestCreateHash(t *testing.T) {
hashRegex, err := regexp.Compile(`^\$argon2id\$v=19\$m=65536,t=15,p=4\$[A-Za-z0-9+/]{86}\$[A-Za-z0-9+/]{683}$`)
assert.Nil(t, err)
shortHash1, err := createHash(shortPassword, lambdaParams)
assert.Nil(t, err)
shortHash2, err := createHash(shortPassword, lambdaParams)
assert.Nil(t, err)
longHash1, err := createHash(longPassword, lambdaParams)
assert.Nil(t, err)
longHash2, err := createHash(longPassword, lambdaParams)
assert.Nil(t, err)
t.Run("verify hash output matches expected regex for short password", func(t *testing.T) {
assert.True(t, hashRegex.MatchString(shortHash1))
})
t.Run("verify same password produces different hashes for short password", func(t *testing.T) {
assert.NotEqual(t, shortHash1, shortHash2)
})
t.Run("verify hash output matches expected regex for long password", func(t *testing.T) {
assert.True(t, hashRegex.MatchString(longHash1))
})
t.Run("verify same password produces different hashes for long password", func(t *testing.T) {
assert.NotEqual(t, longHash1, longHash2)
})
}
func TestCheckHash(t *testing.T) {
shortHash, err := createHash(shortPassword, lambdaParams)
assert.Nil(t, err)
longHash, err := createHash(longPassword, lambdaParams)
assert.Nil(t, err)
t.Run("verify checkHash works for short password / hash", func(t *testing.T) {
ok, shortParams, checkErr := checkHash(shortPassword, shortHash)
assert.Nil(t, checkErr)
assert.True(t, ok)
assert.Equal(t, *shortParams, *lambdaParams)
})
t.Run("verify checkHash works for long password / hash", func(t *testing.T) {
ok, longParams, checkErr := checkHash(longPassword, longHash)
assert.Nil(t, checkErr)
assert.True(t, ok)
assert.Equal(t, *longParams, *lambdaParams)
})
testPass := "bug"
testHash := "$argon2id$v=19$m=65536,t=1,p=2$UDk0zEuIzbt0x3bwkf8Bgw$ihSfHWUJpTgDvNWiojrgcN4E0pJdUVmqCEdRZesx9tE"
t.Run("verify checkHash with hard coded values", func(t *testing.T) {
ok, _, checkErr := checkHash(testPass, testHash)
assert.True(t, ok)
assert.Nil(t, checkErr)
})
t.Run("verify checkHash fails with tampered hash value", func(t *testing.T) {
tamperedHash := testHash[:len(testHash)-1] + "F" // changed one last character of the hash to an invalid value
ok, _, checkErr := checkHash(testPass, tamperedHash)
assert.False(t, ok)
assert.NotNil(t, checkErr)
})
t.Run("verify err when using wrong argon variant", func(t *testing.T) {
// Hash contains wrong variant
_, _, checkErr := checkHash("pa$$word", "$argon2i$v=19$m=65536,t=1,p=2$mFe3kxhovyEByvwnUtr0ow$nU9AqnoPfzMOQhCHa9BDrQ+4bSfj69jgtvGu/2McCxU")
assert.True(t, errors.Is(checkErr, ErrUnsupportedArgonVersion))
})
}
func TestComparePasswordAndHash(t *testing.T) {
shortPasswordHash, err := createHash(shortPassword, lambdaParams)
assert.Nil(t, err)
longPasswordHash, err := createHash(longPassword, lambdaParams)
assert.Nil(t, err)
t.Run("verify compare with correct short password", func(t *testing.T) {
match, compareErr := comparePasswordAndHash(shortPassword, shortPasswordHash)
assert.Nil(t, compareErr)
assert.True(t, match)
})
t.Run("verify err with wrong password for short password hash", func(t *testing.T) {
match, compareErr := comparePasswordAndHash(otherPassword, shortPasswordHash)
assert.Nil(t, compareErr)
assert.False(t, match)
})
t.Run("verify compare with correct long password", func(t *testing.T) {
match, compareErr := comparePasswordAndHash(longPassword, longPasswordHash)
assert.Nil(t, compareErr)
assert.True(t, match)
})
t.Run("verify err with wrong password for long password", func(t *testing.T) {
match, compareErr := comparePasswordAndHash(otherPassword, longPasswordHash)
assert.Nil(t, compareErr)
assert.False(t, match)
})
}
func TestDecodeHash(t *testing.T) {
shortHash, err := createHash(shortPassword, lambdaParams)
assert.Nil(t, err)
longHash, err := createHash(longPassword, lambdaParams)
assert.Nil(t, err)
shortParams, shortSalt, shortKey, err := decodeHash(shortHash)
assert.Nil(t, err)
longParams, longSalt, longKey, err := decodeHash(longHash)
assert.Nil(t, err)
t.Run("verify shortParams returned are correct", func(t *testing.T) {
assert.Equal(t, *shortParams, *lambdaParams)
})
t.Run("verify shortSalt length is correct", func(t *testing.T) {
assert.Equal(t, uint32(len(shortSalt)), lambdaParams.SaltLength)
})
t.Run("verify shortKey length is correct", func(t *testing.T) {
assert.Equal(t, uint32(len(shortKey)), lambdaParams.KeyLength)
})
t.Run("verify longParams returned are correct", func(t *testing.T) {
assert.Equal(t, *longParams, *lambdaParams)
})
t.Run("verify longSalt length is correct", func(t *testing.T) {
assert.Equal(t, uint32(len(longSalt)), lambdaParams.SaltLength)
})
t.Run("verify longKey length is correct", func(t *testing.T) {
assert.Equal(t, uint32(len(longKey)), lambdaParams.KeyLength)
})
}
func TestHash(t *testing.T) {
t.Run("verify password hashes without error", func(t *testing.T) {
goodHash, hashErr := Hash(goodPassword)
assert.Nil(t, hashErr)
assert.NotEqual(t, "", goodHash)
})
}
func TestMatch(t *testing.T) {
t.Run("verify password matches without error", func(t *testing.T) {
goodHash, hashErr := Hash(goodPassword)
assert.Nil(t, hashErr)
assert.NotEqual(t, "", goodHash)
match, matchErr := Match(goodPassword, goodHash)
assert.Nil(t, matchErr)
assert.True(t, match)
})
}
func TestVerifyPasswordRequirements(t *testing.T) {
t.Run("verify error when password is below minimum", func(t *testing.T) {
err := verifyPasswordRequirements(generateRandomString(MinPasswordLength - 1))
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrPasswordTooShort))
})
t.Run("verify error when password is above maximum", func(t *testing.T) {
err := verifyPasswordRequirements(generateRandomString(MaxPasswordLength + 1))
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrPasswordTooLong))
})
t.Run("verify no error when input is minimum", func(t *testing.T) {
err := verifyPasswordRequirements(generateRandomString(MinPasswordLength))
assert.Nil(t, err)
})
t.Run("verify no error when input is maximum", func(t *testing.T) {
err := verifyPasswordRequirements(generateRandomString(MaxPasswordLength))
assert.Nil(t, err)
})
}
func generateRandomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
rand.Seed(time.Now().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}