-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shadow_store.go
42 lines (34 loc) · 1.17 KB
/
shadow_store.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
package server
import "github.com/ecnepsnai/secutil"
// Compare will return true if the saved hash for the given username matches the provided raw password
func (s *shadowStoreObject) Compare(username string, raw []byte) bool {
hashedPwBytes := s.Store.Get(username)
if len(hashedPwBytes) == 0 {
log.Warn("No shadow entry: username='%s'", username)
return false
}
hashedPw := secutil.HashedPassword(hashedPwBytes)
return hashedPw.Compare(raw)
}
// Set will save the given hash for the username
func (s *shadowStoreObject) Set(username string, hash secutil.HashedPassword) {
s.Store.Write(username, hash)
}
// Delete will remove any saved hash for the username
func (s *shadowStoreObject) Delete(username string) {
s.Store.Delete(username)
}
// Upgrade will upgrade this password with an improved algorithm if needed
func (s *shadowStoreObject) Upgrade(username string, raw []byte) {
hashedPwBytes := s.Store.Get(username)
if len(hashedPwBytes) == 0 {
log.Warn("No shadow entry: username='%s'", username)
return
}
hashedPw := secutil.HashedPassword(hashedPwBytes)
newHashedPw := hashedPw.Upgrade(raw)
if newHashedPw == nil {
return
}
s.Set(username, *newHashedPw)
}