Base Project
This commit is contained in:
22
pkg/helper/password.go
Normal file
22
pkg/helper/password.go
Normal file
@@ -0,0 +1,22 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user