-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_credential_store_peppered_test.go
149 lines (129 loc) · 3.82 KB
/
example_credential_store_peppered_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
package passhash_test
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
)
import (
"github.com/dhui/passhash"
)
var pepperGrinder PepperGrinder
type PepperGrinder struct {
cipher cipher.Block
}
func (p PepperGrinder) Encrypt(text []byte) (nonce, encrypted []byte) {
nonce = make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}
aesgcm, err := cipher.NewGCM(p.cipher)
if err != nil {
panic(err.Error())
}
return nonce, aesgcm.Seal(nil, nonce, text, nil)
}
func (p PepperGrinder) Decrypt(nonce, text []byte) []byte {
aesgcm, err := cipher.NewGCM(p.cipher)
if err != nil {
panic(err.Error())
}
decryptedText, err := aesgcm.Open(nil, nonce, text, nil)
if err != nil {
panic(err.Error())
}
return decryptedText
}
func NewPepperGrinder(key []byte) (PepperGrinder, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
return PepperGrinder{}, err
}
return PepperGrinder{cipher}, nil
}
func init() {
var err error
pepperGrinder, err = NewPepperGrinder(KeyForPepperID[DefaultPepperID])
if err != nil {
panic(err.Error())
}
}
type PepperID uint
const DefaultPepperID PepperID = 1
var KeyForPepperID = map[PepperID][]byte{
1: []byte("AES256Key-32Characters1234567890"),
}
type StringCredentialPepperedStore struct {
StringCredentialStore
PepperID PepperID // Versions pepper key/method
Nonce []byte
}
func (store *StringCredentialPepperedStore) Store(credential *passhash.Credential) error {
store.PepperID = DefaultPepperID
store.Nonce, credential.Hash = pepperGrinder.Encrypt(credential.Hash)
return store.StringCredentialStore.Store(credential)
}
func (store *StringCredentialPepperedStore) StoreContext(ctx context.Context,
credential *passhash.Credential) error {
return store.Store(credential)
}
func (store *StringCredentialPepperedStore) Load(id passhash.UserID) (*passhash.Credential, error) {
credential, err := store.StringCredentialStore.Load(id)
if err != nil {
return nil, err
}
switch store.PepperID {
case 1:
credential.Hash = pepperGrinder.Decrypt(store.Nonce, credential.Hash)
default:
return nil, fmt.Errorf("Unsupported PepperID %v", store.PepperID)
}
return credential, nil
}
func (store *StringCredentialPepperedStore) LoadContext(id passhash.UserID) (*passhash.Credential, error) {
return store.Load(id)
}
// nolint: dupl
func ExampleCredentialStore_peppered() {
userID := passhash.UserID(0)
password := "insecurepassword"
origCredential, err := passhash.NewCredential(userID, password)
if err != nil {
fmt.Println("Error creating credential.", err)
return
}
store := StringCredentialPepperedStore{}
if err := store.Store(origCredential); err != nil {
fmt.Println("Error storing credential.", err)
return
}
newCredential, err := store.Load(userID)
if err != nil {
fmt.Println("Error loading credential.", err)
return
}
credentialEqual := newCredential == origCredential
kdfEqual := newCredential.Kdf == origCredential.Kdf
cfEqual := newCredential.WorkFactor == origCredential.WorkFactor // Not equal due to pointer comparison
saltEqual := bytes.Equal(newCredential.Salt, origCredential.Salt)
hashEqual := bytes.Equal(newCredential.Hash, origCredential.Hash)
matched, updated := newCredential.MatchesPassword(password)
fmt.Println("credentialEqual:", credentialEqual)
fmt.Println("kdfEqual:", kdfEqual)
fmt.Println("cfEqual:", cfEqual)
fmt.Println("saltEqual:", saltEqual)
fmt.Println("hashEqual:", hashEqual) // Not equal due to peppering
fmt.Println("newCredential.MatchesPassword (matched):", matched)
fmt.Println("newCredential.MatchesPassword (updated):", updated)
// Output:
// credentialEqual: false
// kdfEqual: true
// cfEqual: false
// saltEqual: true
// hashEqual: false
// newCredential.MatchesPassword (matched): true
// newCredential.MatchesPassword (updated): false
}