Files
warehouse-management-BE/pkg/helper/password.go
Tran Anh Tuan 6a4a96e0ca Base Project
2026-05-08 14:32:24 +07:00

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
}