server/internal/core/utils/password.go
2024-10-13 13:31:58 +04:00

27 lines
620 B
Go

package utils
import (
"golang.org/x/crypto/bcrypt"
)
// HashPassword hashes password and returns hashed password or error
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 password with hashed password and returns error if they don't match or nil if they do
func ComparePassword(password, hashedPassword string) error {
return bcrypt.CompareHashAndPassword(
[]byte(hashedPassword),
[]byte(password),
)
}