Base Project

This commit is contained in:
Tran Anh Tuan
2026-05-08 14:32:24 +07:00
parent 5a9249c9ea
commit 6a4a96e0ca
74 changed files with 6749 additions and 0 deletions

22
pkg/helper/password.go Normal file
View 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
}