forked from shaj13/go-guardian
/
comparator.go
52 lines (42 loc) · 1.12 KB
/
comparator.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
package basic
import (
"crypto"
"crypto/subtle"
"encoding/hex"
)
// Comparator is the interface implemented by types,
// that can generate password hash and compares the hashed password
// with its possible plaintext equivalent
type Comparator interface {
Hash(password string) (string, error)
Compare(hashedPassword, password string) error
}
type basicHashing struct {
h crypto.Hash
}
func (b basicHashing) Hash(password string) (string, error) {
hasher := b.h.New()
_, _ = hasher.Write([]byte(password))
sum := hasher.Sum(nil)
return hex.EncodeToString(sum), nil
}
func (b basicHashing) Compare(hashedPassword, password string) error {
hash, err := b.Hash(password)
if err != nil {
return err
}
if subtle.ConstantTimeCompare([]byte(hash), []byte(hashedPassword)) == 1 {
return nil
}
return ErrInvalidCredentials
}
type plainText struct{}
func (p plainText) Hash(password string) (string, error) {
return password, nil
}
func (p plainText) Compare(hashedPassword, password string) error {
if subtle.ConstantTimeCompare([]byte(hashedPassword), []byte(password)) == 1 {
return nil
}
return ErrInvalidCredentials
}