Files
photodisk/internal/auth/auth.go
Artem Mamonov acf9b43671 Initial commit
2025-02-06 02:36:10 +01:00

34 lines
641 B
Go

package auth
import "golang.org/x/crypto/bcrypt"
var (
ErrEmptyPassword = Error{"password is empty"}
ErrPasswordIncorrect = Error{"password is incorrect"}
)
type Error struct {
Err string
}
func (e Error) Error() string {
return e.Err
}
func HashPassword(password string) (string, error) {
if password == "" {
return "", ErrEmptyPassword
}
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(bytes), err
}
func CheckPasswordHash(password, hash string) error {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err != nil {
return ErrPasswordIncorrect
}
return nil
}