|
4 | 4 | // Maintainer: Kevin Marville |
5 | 5 |
|
6 | 6 | import 'dart:convert'; |
| 7 | +import 'dart:math'; |
7 | 8 | import 'package:crypto/crypto.dart'; |
8 | 9 | import 'package:validators/validators.dart'; |
9 | 10 | import 'package:email_validator/email_validator.dart'; |
10 | 11 |
|
11 | 12 | /// Classe principale pour la validation et sanitization des entrées |
12 | 13 | class InputValidator { |
| 14 | + static final Random _secureRandom = Random.secure(); |
13 | 15 | // Expressions régulières pour la validation |
14 | 16 | static final RegExp _nameRegex = RegExp(r'^[a-zA-ZÀ-ÿ\s\-\']{2,50}$'); |
15 | 17 | static final RegExp _passwordRegex = RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$'); |
@@ -243,9 +245,18 @@ class InputValidator { |
243 | 245 |
|
244 | 246 | /// Génère un token sécurisé |
245 | 247 | 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); |
249 | 260 | } |
250 | 261 |
|
251 | 262 | /// Hash un mot de passe avec salt |
|
0 commit comments