feat: remove bcrypt and use a precompute hash

This commit is contained in:
2025-11-21 16:11:08 +01:00
parent ff471cd5dd
commit 4cdda3f944

View File

@@ -9,7 +9,6 @@ import (
"sync"
"time"
"golang.org/x/crypto/bcrypt"
"goyco/internal/database"
"goyco/internal/repositories"
)
@@ -17,6 +16,7 @@ import (
type ParallelProcessor struct {
maxWorkers int
timeout time.Duration
passwordHash string
}
func NewParallelProcessor() *ParallelProcessor {
@@ -28,6 +28,10 @@ func NewParallelProcessor() *ParallelProcessor {
}
}
func (p *ParallelProcessor) SetPasswordHash(hash string) {
p.passwordHash = hash
}
func (p *ParallelProcessor) CreateUsersInParallel(userRepo repositories.UserRepository, count int, progress *ProgressIndicator) ([]database.User, error) {
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
@@ -292,18 +296,12 @@ func generateRandomIdentifier() string {
}
func (p *ParallelProcessor) createSingleUser(userRepo repositories.UserRepository, index int) (database.User, error) {
password := "password123"
randomID := generateRandomIdentifier()
username := fmt.Sprintf("user_%s", randomID)
email := fmt.Sprintf("user_%s@goyco.local", randomID)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return database.User{}, fmt.Errorf("hash password: %w", err)
}
const maxRetries = 10
for attempt := 0; attempt < maxRetries; attempt++ {
for range maxRetries {
user, err := userRepo.GetByEmail(email)
if err == nil {
return *user, nil
@@ -312,7 +310,7 @@ func (p *ParallelProcessor) createSingleUser(userRepo repositories.UserRepositor
user = &database.User{
Username: username,
Email: email,
Password: string(hashedPassword),
Password: p.passwordHash,
EmailVerified: true,
}
@@ -379,7 +377,7 @@ func (p *ParallelProcessor) createSinglePost(postRepo repositories.PostRepositor
content := fmt.Sprintf("Autogenerated seed post #%d\n\nThis is sample content for testing purposes. The post discusses %s and provides valuable insights.", index, title)
const maxRetries = 10
for attempt := 0; attempt < maxRetries; attempt++ {
for range maxRetries {
post := &database.Post{
Title: title,
URL: url,