-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck_test.go
127 lines (112 loc) · 3.21 KB
/
check_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
package hashers
import (
"testing"
)
const password = "secret"
var passwords = map[string]string{
"pbkdf2_sha256": "pbkdf2_sha256$20000$xHAwgryJD2q2$PqZjhRe60ZCfa2fBDI7prOhst33qaeHoYSgsaRfiMDE=",
"pbkdf2_sha1": "pbkdf2_sha1$20000$eZ5FzW0FNpxe$dJlO62dNkeUQBXh5y+mG3cAaeCo=",
"sha1": "sha1$Lj8mOndIS539$354aa09b3c8305590aba9d61ea2eea695e66a2f3",
"md5": "md5$AnJwur40VmOL$b6adea34c4639397cc5defc10261837c",
"unsalted_sha1": "sha1$$e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4",
"unsalted_md5": "5ebe2294ecd0e0f08eab7690d2a6ee69",
}
func TestCheckPasswordPbkdf2Sha256(t *testing.T) {
ok, err := CheckPassword(password, passwords["pbkdf2_sha256"])
if err != nil {
t.Error(err)
} else if ok != true {
t.Error("Password doesn't match the hash")
}
}
func TestCheckPasswordErr(t *testing.T) {
_, err := CheckPassword("", "pbkdf2_sha256")
if err == nil {
t.Error("Wrong hash syntax should return an error.")
}
}
func TestCheckPassworWrongIterations(t *testing.T) {
_, err := CheckPassword("", "pbkdf2_sha256$not-integer$$")
if err == nil {
t.Error("Wrong number of iterations should return an error.")
}
}
func TestCheckPassworWrongHash(t *testing.T) {
_, err := CheckPassword("", "pbkdf2_sha256$20000$$not+base64")
if err == nil {
t.Error("Wrong hash encoding should return an error.")
}
}
func TestCheckPassworWrongAlgorithm(t *testing.T) {
_, err := CheckPassword("", "wrong$$$")
if err == nil {
t.Error("Wrong algorithm should return an error.")
}
}
func TestCheckPasswordPbkdf2Sha1(t *testing.T) {
ok, err := CheckPassword(password, passwords["pbkdf2_sha1"])
if err != nil {
t.Error(err)
} else if ok != true {
t.Error("Password doesn't match the hash")
}
}
func TestCheckPasswordIncomplete(t *testing.T) {
encoded := passwords["sha1"]
_, err := CheckPassword(password, encoded[:len(encoded)-2])
if err == nil {
t.Error("Incomplete hash should return an error.")
}
}
func TestCheckPasswordSaltedSha1(t *testing.T) {
ok, err := CheckPassword(password, passwords["sha1"])
if err != nil {
t.Error(err)
} else if ok != true {
t.Error("Password doesn't match the hash")
}
}
func TestCheckPasswordSaltedMd5(t *testing.T) {
ok, err := CheckPassword(password, passwords["md5"])
if err != nil {
t.Error(err)
} else if ok != true {
t.Error("Password doesn't match the hash")
}
}
func TestCheckPasswordUnsaltedSha1(t *testing.T) {
ok, err := CheckPassword(password, passwords["unsalted_sha1"])
if err != nil {
t.Error(err)
} else if ok != true {
t.Error("Password doesn't match the hash")
}
}
func TestCheckPasswordUnsaltedMd5(t *testing.T) {
ok, err := CheckPassword(password, passwords["unsalted_md5"])
if err != nil {
t.Error(err)
} else if ok != true {
t.Error("Password doesn't match the hash")
}
}
func BenchmarkCheckCorrectPassword(b *testing.B) {
for hasher, encoded := range passwords {
DefaultHasher = hasher
b.Run(hasher, func(b *testing.B) {
for i := 0; i < b.N; i++ {
CheckPassword(password, encoded)
}
})
}
}
func BenchmarkCheckIncorrectPassword(b *testing.B) {
for hasher, encoded := range passwords {
DefaultHasher = hasher
b.Run(hasher, func(b *testing.B) {
for i := 0; i < b.N; i++ {
CheckPassword(password[1:], encoded)
}
})
}
}