-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
51 lines (36 loc) · 1.2 KB
/
encrypt.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
package auth
import (
"golang.org/x/crypto/bcrypt"
)
/*---------------------------------
Interface
----------------------------------*/
// EncryptionManager describes all of the methods used
// for handling the encryption side of our auth layer
type EncryptionManager interface {
HashPassword(password string) (string, error)
CheckPassword(hashed string, password string) (bool, error)
}
/*---------------------------------
Method Implementations
----------------------------------*/
// HashPassword takes a given password and hashes it for storage in the user table
func (a *Auth) HashPassword(password string) (string, error) {
bytePassword := []byte(password)
hash, err := bcrypt.GenerateFromPassword(bytePassword, bcrypt.MinCost)
if err != nil {
return "", err
}
return string(hash), nil
}
// CheckPassword checks a given plain password against
// a hashed password to see if they're compatible
func (a *Auth) CheckPassword(hashed string, password string) (bool, error) {
byteHash := []byte(hashed)
bytePassword := []byte(password)
err := bcrypt.CompareHashAndPassword(byteHash, bytePassword)
if err != nil {
return false, err
}
return true, nil
}