22 lines
687 B
Go
22 lines
687 B
Go
package auth
|
|
|
|
import "golang.org/x/crypto/bcrypt"
|
|
|
|
// BcryptCost is kept in one place so all services use the same password
|
|
// storage policy. Existing hashes remain valid when this value is raised.
|
|
const BcryptCost = 12
|
|
|
|
// HashPassword returns a bcrypt password hash suitable for storage.
|
|
func HashPassword(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), BcryptCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(hash), nil
|
|
}
|
|
|
|
// VerifyPassword checks a plaintext password against a stored bcrypt hash.
|
|
func VerifyPassword(hash, password string) bool {
|
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
|
}
|