Files
backend/internal/pkg/password/password.go
2025-12-13 22:34:01 +05:00

37 lines
977 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}