Initial commit
This commit is contained in:
33
internal/auth/auth.go
Normal file
33
internal/auth/auth.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user