Hosted preview: results below were recorded from real runs of the demo on a local machine. Nothing runs on this server. To try your own payloads, run it locally with npm run demo.

nodejs-crypto-secrets

Common crypto mistakes in Node.js, run live next to their fixes. View on GitHub

Recorded runs · nothing executes on this server

Password storage

An unsalted SHA-256 hash is the same for everyone who picks the same password, so attackers look it up in a precomputed list. Salted scrypt is different every time and slow to guess.

Try:

Fix: crypto.scrypt (or Argon2id) with a random salt per user, compared with timingSafeEqual.

Random tokens

Math.random() is built for speed, not secrecy. Its internal state can be recovered from a few outputs, after which every "random" reset link is predictable.

Fix: crypto.randomBytes(32) (or randomUUID, randomInt), and store only a hash of the token.

JWT verification

Edit a token and send it to two verifiers. The unsafe one lets the token pick its own algorithm ("none" means no signature check) and ignores expiry.

Try:

Fix: pin the algorithm on the server, compare signatures in constant time, and enforce exp.

Encryption

AES-CBC with a fixed IV encrypts the same message to the same bytes, and has no integrity check: editing the ciphertext silently edits the decrypted message.

Try:

Fix: AES-256-GCM with a fresh random IV per message. The auth tag makes any edit fail to decrypt.

Secrets & config

The value of JWT_SECRET the app starts with. The unsafe pattern falls back to a default and logs its config; the safe one refuses weak secrets and redacts logs.

Try:

Fix: load secrets from the environment, fail at startup if missing or short, and redact before logging.