Criptografía aplicada
Aleatoriedad segura
Math.random y java.util.Random son predecibles. Para tokens: crypto.randomBytes / SecureRandom.
❌ Lo que sale mal
- Generar tokens de sesión o reset con
Math.random()/java.util.Random. - Sembrar un PRNG con la hora (predecible).
- Reusar "aleatorios" entre pedidos.
✅ Cómo se hace bien
- Para seguridad, CSPRNG:
crypto.randomBytes(Node),SecureRandom(Java),secrets(Python). - Suficiente entropía (≥128 bits) para tokens.
- Nunca mezclar el generador "rápido" de simulaciones con el de seguridad.
❌ Predecible
const token = Math.random().toString(36).slice(2); // NO para tokens/reset/OTP✅ Seguro
crypto.randomBytes(32).toString("base64url"); // Node
crypto.randomUUID(); // Node y navegador
// Java: new SecureRandom().nextBytes(bytes);