-
Notifications
You must be signed in to change notification settings - Fork 4
/
passhash_test.go
113 lines (102 loc) · 3.35 KB
/
passhash_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
package passhash_test
import (
"testing"
)
import (
"github.com/dhui/passhash"
)
func TestNewCredential(t *testing.T) {
userID := passhash.UserID(0)
credential, err := passhash.NewCredential(userID, testPassword)
if err != nil {
t.Fatalf("Failed to get password credential. Got error %v", err)
}
if matched, _ := credential.MatchesPassword(testPassword); !matched {
t.Errorf("Password did not match Credential")
}
if matched, _ := credential.MatchesPassword(testPassword + "extra"); matched {
t.Errorf("Password matched Credential")
}
}
func TestNewWorkFactorForKdfPbkdf2Sha256(t *testing.T) {
wf, err := passhash.NewWorkFactorForKdf(passhash.Pbkdf2Sha256)
if err != nil {
t.Error("Got error getting WorkFactor for Pbkdf2Sha256", err)
}
if _, ok := wf.(*passhash.Pbkdf2WorkFactor); !ok {
t.Error("Expected Pbkdf2Sha256 KDF to have a Pbkdf2WorkFactor WorkFactor")
}
}
func TestNewWorkFactorForKdfPbkdf2Sha512(t *testing.T) {
wf, err := passhash.NewWorkFactorForKdf(passhash.Pbkdf2Sha512)
if err != nil {
t.Error("Got error getting WorkFactor for Pbkdf2Sha512", err)
}
if _, ok := wf.(*passhash.Pbkdf2WorkFactor); !ok {
t.Error("Expected Pbkdf2Sha512 KDF to have a Pbkdf2WorkFactor WorkFactor")
}
}
func TestNewWorkFactorForKdfPbkdf2Sha3_256(t *testing.T) {
wf, err := passhash.NewWorkFactorForKdf(passhash.Pbkdf2Sha3_256)
if err != nil {
t.Error("Got error getting WorkFactor for Pbkdf2Sha3_256", err)
}
if _, ok := wf.(*passhash.Pbkdf2WorkFactor); !ok {
t.Error("Expected Pbkdf2Sha3_256 KDF to have a Pbkdf2WorkFactor WorkFactor")
}
}
func TestNewWorkFactorForKdfPbkdf2Sha3_512(t *testing.T) {
wf, err := passhash.NewWorkFactorForKdf(passhash.Pbkdf2Sha3_512)
if err != nil {
t.Error("Got error getting WorkFactor for Pbkdf2Sha3_512", err)
}
if _, ok := wf.(*passhash.Pbkdf2WorkFactor); !ok {
t.Error("Expected Pbkdf2Sha3_512 KDF to have a Pbkdf2WorkFactor WorkFactor")
}
}
func TestNewWorkFactorForKdfBcrypt(t *testing.T) {
wf, err := passhash.NewWorkFactorForKdf(passhash.Bcrypt)
if err != nil {
t.Error("Got error getting WorkFactor for Bcrypt", err)
}
if _, ok := wf.(*passhash.BcryptWorkFactor); !ok {
t.Error("Expected Bcrypt KDF to have a BcryptWorkFactor WorkFactor")
}
}
func TestNewWorkFactorForKdfScrypt(t *testing.T) {
wf, err := passhash.NewWorkFactorForKdf(passhash.Scrypt)
if err != nil {
t.Error("Got error getting WorkFactor for Scrypt", err)
}
if _, ok := wf.(*passhash.ScryptWorkFactor); !ok {
t.Error("Expected Scrypt KDF to have a ScryptWorkFactor WorkFactor")
}
}
func TestNewWorkFactorError(t *testing.T) {
_, err := passhash.NewWorkFactorForKdf(passhash.Kdf(999999))
if err == nil {
t.Error("Expected error for invalid Kdf")
}
}
func Example() {
credential, err := passhash.NewCredential(passhash.UserID(0), testPassword)
if err != nil {
// Handle error gettings credential
}
matched, updated := credential.MatchesPassword(testPassword)
if !matched {
// Handle invalid password
}
if updated {
// store := SomeStorage() // SomeStorage implements the CredentialStore interface
// store.Marshal(credential)
}
newPassword := "newinsecurepassword"
if err = credential.ChangePassword(testPassword, newPassword); err != nil {
// Handle PasswordPoliciesNotMet error
}
newPassword2 := "newinsecurepassword2"
if err = credential.Reset(newPassword2); err != nil {
// Handle PasswordPoliciesNotMet error
}
}