Files
tp/serv_golang_rest_api/internal/utils/password.go
T
valitovgaziz bf296373bd modified: go.mod
modified:   go.sum
	new file:   internal/config/oauth.go
	new file:   internal/handler/auth.go
	new file:   internal/utils/errors.go
	new file:   internal/utils/json.go
	modified:   internal/utils/password.go
Add utils, add auth handler, auth configs
2025-09-29 01:28:22 +05:00

30 lines
823 B
Go

// utils/password.go
package utils
import (
"crypto/rand"
"encoding/base64"
"golang.org/x/crypto/bcrypt"
)
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// GenerateRandomPassword генерирует случайный пароль для OAuth пользователей
func GenerateRandomPassword() string {
bytes := make([]byte, 32) // 256 бит
_, err := rand.Read(bytes)
if err != nil {
// Fallback - используем временный пароль
return "temp_oauth_password_123"
}
return base64.URLEncoding.EncodeToString(bytes)
}