forked from hlandau/passlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
71 lines (54 loc) · 1.6 KB
/
example_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
package passlib
// User signup example.
func ExampleHash_signup() {
// User signup example.
// NOTE: Call UseDefaults at application initialisation time to initialise
// passlib before using Hash() or Verify(). See func UseDefaults.
// ... signup code ...
// Get the password the user chose by whatever means.
password := getSignupPassword()
username := getSignupUsername()
hash, err := Hash(password)
if err != nil {
// couldn't hash password for some reason
return
}
// hash now contains a hash in modular crypt form.
// Store hash in database, etc.
storeHashInDatabase(username, hash)
}
// User login example.
func ExampleVerify_login() {
// User login example.
// NOTE: Call UseDefaults at application initialisation time to initialise
// passlib before using Hash() or Verify(). See func UseDefaults.
// Get the password for the user we have stored in the database.
hash := getUserHashFromDatabase()
// Get the plaintext password the user tried to login with.
password := getLoginPassword()
newHash, err := Verify(password, hash)
if err != nil {
// Incorrect password, malformed hash, etc.
return
}
if newHash != "" {
// passlib thinks we should upgrade to a new stronger hash.
// ... store the new hash in the database ...
}
// ... log the user in ...
}
// These are dummy functions for the benefit of the examples.
func getSignupUsername() string {
return ""
}
func getSignupPassword() string {
return "password"
}
func getLoginPassword() string {
return "password"
}
func storeHashInDatabase(username, hash string) {
}
func getUserHashFromDatabase() string {
return ""
}