password.go
18 lines1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package security
import "golang.org/x/crypto/bcrypt"
// HashPassword hashes a password with bcrypt.
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hash), nil
}
// CheckPassword compares a bcrypt hash with a plaintext password.
// Returns true if they match.
func CheckPassword(hash, password string) bool {
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
}