hiling_go/util/password.go

20 lines
475 B
Go
Raw Normal View History

2023-09-08 22:25:22 +07:00
package util
import (
"fmt"
"golang.org/x/crypto/bcrypt"
)
func HashPassword(password string) (string, error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
2023-09-12 17:07:57 +07:00
return "", fmt.Errorf("failed to hash password: %w", err)
2023-09-08 22:25:22 +07:00
}
return string(hashedPassword), nil
}
func CheckPassword(password string, hashedPassword string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}