initial commit
This commit is contained in:
36
internal/pkg/password/password.go
Normal file
36
internal/pkg/password/password.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultCost - стандартная стоимость хеширования bcrypt
|
||||
DefaultCost = bcrypt.DefaultCost
|
||||
)
|
||||
|
||||
// Hash хеширует пароль с использованием bcrypt
|
||||
func Hash(password string) (string, error) {
|
||||
if password == "" {
|
||||
return "", fmt.Errorf("password cannot be empty")
|
||||
}
|
||||
|
||||
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), DefaultCost)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to hash password: %w", err)
|
||||
}
|
||||
|
||||
return string(hashedBytes), nil
|
||||
}
|
||||
|
||||
// Verify проверяет соответствие пароля хешу
|
||||
func Verify(hashedPassword, password string) error {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
|
||||
}
|
||||
|
||||
// IsValid проверяет валидность пароля (минимальная длина)
|
||||
func IsValid(password string) bool {
|
||||
return len(password) >= 8
|
||||
}
|
||||
Reference in New Issue
Block a user