Weak crypto (CWE-327/338): no MD5/SHA-1 for passwords, no Math.random() for tokens
MD5 and SHA-1 are fast hashes, built for integrity checks, not for passwords — they are brute-forced at billions of guesses per second on commodity GPUs. Use bcrypt/argon2/scrypt for passwords, and crypto.randomBytes / secrets.token_hex for anything security-sensitive like tokens or session ids.
Weak cryptographic choices (CWE-327, CWE-338)
Two unrelated mistakes that share a root cause — using a general-purpose, fast algorithm where a slow, purpose-built one is required.
Password hashing: not md5/sha1/sha256 alone
MD5 and SHA-1 are fast — that is exactly wrong for password storage, because it's also fast for an attacker who stole the hash and is brute-forcing it offline. Plain SHA-256 has the same problem (fast, no built-in salt).
// BAD — CWE-327: fast general-purpose hash, brute-forceable at scale
const hash = crypto.createHash('md5').update(password).digest('hex');
const hash2 = crypto.createHash('sha1').update(password).digest('hex');
// GOOD — bcrypt: built-in salt, deliberately slow, a cost factor you can raise over time
import bcrypt from 'bcrypt';
const hash = await bcrypt.hash(password, 12);
const ok = await bcrypt.compare(candidate, hash);
argon2 (the current OWASP-recommended default) and scrypt are the other acceptable choices; all three are deliberately slow and memory-hard, which is the actual requirement for password storage.
# BAD
hashlib.md5(password.encode()).hexdigest()
hashlib.sha1(password.encode()).hexdigest()
# GOOD
import bcrypt
hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
MD5/SHA-1/SHA-256 are still the right tool for non-security checksums — verifying a file didn't get corrupted in transit, content-addressed caching, etc. The rule is about what the hash protects, not the algorithm in a vacuum.
Random values: not Math.random() for anything security-sensitive
Math.random() (and Python's random module) use a non-cryptographic PRNG — given enough outputs, its internal
state can be predicted, which means a session token or password-reset code generated this way can be guessed or
reconstructed.
// BAD — CWE-338: predictable PRNG used for a security-sensitive value
const token = Math.random().toString(36).slice(2);
// GOOD — cryptographically secure randomness
import { randomBytes } from 'node:crypto';
const token = randomBytes(32).toString('hex');
# BAD
token = str(random.random())
# GOOD
import secrets
token = secrets.token_hex(32)
Math.random() is fine for things that don't need to resist a deliberate guesser — shuffling a UI list, a
non-security sample ID.
What codexguild_scan_security checks
The weak-crypto rule flags createHash('md5'|'sha1') and hashlib.md5()/hashlib.sha1() wherever they
appear — it can't tell a password-hashing use from a checksum use from the line alone, so treat a hit as "worth
checking what this hash protects," not an automatic rewrite.