23 lines
836 B
Go
23 lines
836 B
Go
package helper
|
|
|
|
import "golang.org/x/crypto/bcrypt"
|
|
|
|
// HashPassword generates a salted and hashed password using bcrypt.
|
|
// It takes the plain-text password and the number of salt rounds as input.
|
|
// It returns the generated salt, the hashed password, and any error encountered.
|
|
func HashPassword(password string) (string, error) {
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(hashedPassword), nil
|
|
}
|
|
|
|
// ComparePassword compares a plain-text password with a hashed password and returns true if they match.
|
|
// It uses bcrypt.CompareHashAndPassword to perform the comparison.
|
|
func ComparePassword(password string, hashedPassword string) error {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
|
return err
|
|
}
|