Skip to content

Commit 44850f6

Browse files
authored
Merge pull request #11 from Kvnbbg/codex/refine-code-review-scope-and-strategy
Improve secure token generation
2 parents de58f7f + 18abb05 commit 44850f6

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

input_validator.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
// Maintainer: Kevin Marville
55

66
import 'dart:convert';
7+
import 'dart:math';
78
import 'package:crypto/crypto.dart';
89
import 'package:validators/validators.dart';
910
import 'package:email_validator/email_validator.dart';
1011

1112
/// Classe principale pour la validation et sanitization des entrées
1213
class InputValidator {
14+
static final Random _secureRandom = Random.secure();
1315
// Expressions régulières pour la validation
1416
static final RegExp _nameRegex = RegExp(r'^[a-zA-ZÀ-ÿ\s\-\']{2,50}$');
1517
static final RegExp _passwordRegex = RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$');
@@ -243,9 +245,18 @@ class InputValidator {
243245

244246
/// Génère un token sécurisé
245247
static String generateSecureToken([int length = 32]) {
246-
final bytes = List<int>.generate(length, (i) =>
247-
DateTime.now().millisecondsSinceEpoch + i);
248-
return sha256.convert(bytes).toString().substring(0, length);
248+
if (length <= 0) {
249+
throw ArgumentError.value(length, 'length', 'Token length must be positive');
250+
}
251+
252+
final buffer = StringBuffer();
253+
254+
while (buffer.length < length) {
255+
final bytes = List<int>.generate(32, (_) => _secureRandom.nextInt(256));
256+
buffer.write(base64UrlEncode(bytes).replaceAll('=', ''));
257+
}
258+
259+
return buffer.toString().substring(0, length);
249260
}
250261

251262
/// Hash un mot de passe avec salt

0 commit comments

Comments
 (0)