forked from alexedwards/argon2id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargon2id_test.go
57 lines (46 loc) · 1.03 KB
/
argon2id_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
package argon2id
import (
"regexp"
"strings"
"testing"
)
func TestCreateHash(t *testing.T) {
hashRX, err := regexp.Compile(`^\$argon2id\$v=19\$m=65536,t=3,p=2\$[A-Za-z0-9+/]{22}\$[A-Za-z0-9+/]{43}$`)
if err != nil {
t.Fatal(err)
}
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")
}
}
func TestComparePasswordAndHash(t *testing.T) {
hash, err := CreateHash("pa$$word", DefaultParams)
if err != nil {
t.Fatal(err)
}
match, err := ComparePasswordAndHash("pa$$word", hash)
if err != nil {
t.Fatal(err)
}
if !match {
t.Error("expected password and hash to match")
}
match, err = ComparePasswordAndHash("otherPa$$word", hash)
if err != nil {
t.Fatal(err)
}
if match {
t.Error("expected password and hash to not match")
}
}