diff --git a/internal/interfaces/utils.go b/internal/interfaces/utils.go index 35b495d..d7c06b5 100644 --- a/internal/interfaces/utils.go +++ b/internal/interfaces/utils.go @@ -5,4 +5,6 @@ import "github.com/gin-gonic/gin" type Utils interface { IsEmail(email string) bool GetUserUuidFromContext(c *gin.Context) (string, error) + HashPassword(password string) (string, error) + ComparePasswords(hashedPassword string, plainPassword string) error } diff --git a/pkg/utils/password.go b/pkg/utils/password.go new file mode 100644 index 0000000..fa4e08e --- /dev/null +++ b/pkg/utils/password.go @@ -0,0 +1,15 @@ +package utils + +import "golang.org/x/crypto/bcrypt" + +func (u *Utils) HashPassword(password string) (string, error) { + bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) + if err != nil { + return "", err + } + return string(bytes), nil +} + +func (u *Utils) ComparePasswords(hashedPassword string, plainPassword string) error { + return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(plainPassword)) +}