Initial commit

This commit is contained in:
Artem Mamonov
2025-02-06 02:36:10 +01:00
commit acf9b43671
24 changed files with 1946 additions and 0 deletions

33
internal/auth/auth.go Normal file
View File

@@ -0,0 +1,33 @@
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
}