← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Entropy

general PHP 7.0+ Intermediate
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list semgrep and psalm, and the code_pattern targets specific misuses like mt_srand(time()) or rand() for cryptographic material — these are not caught by default linting but require configured SAST rules or specialist static analysis tooling.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is a direct swap — replace mt_rand/time-based seeds with random_bytes(32). It's more than a one-line patch because all call sites generating tokens/secrets must be located and replaced, but it doesn't span architectural boundaries.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The concept applies to web and CLI contexts where security-sensitive token generation occurs, but it's a bounded concern — only code paths that generate secrets, tokens, or passwords are affected. The rest of the codebase is unaffected.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field directly states the canonical wrong belief: 'a long password always has high entropy.' This contradicts intuitive reasoning (length = strength) in a way that is security-critical. Similarly, common mistakes show that developers measure entropy in characters rather than bits, and that URL-safe encoding silently reduces entropy per character — multiple compounding misconceptions that contradict how developers reason about related concepts like string length and randomness.

About DEBT scoring →

Also Known As

information entropy password entropy cryptographic entropy

TL;DR

A measure of unpredictability in a value — high entropy means many possible values, making brute-force infeasible.

Explanation

In the context of security tokens, entropy is the number of possible values an attacker would have to guess. bin2hex(random_bytes(32)) has 256 bits of entropy — 2²⁵⁶ possible values. md5(time()) has about 17 bits (86,400 seconds per day, rounded to power of 2) — trivially enumerable. More bits of entropy means exponentially more guesses required. For security tokens, 128+ bits of entropy from a CSPRNG is the minimum recommended today.

Common Misconception

A long password always has high entropy. Length contributes to entropy but so does character set and unpredictability — "aaaaaaaaaaaaaaaa" is 16 characters but near-zero entropy. True entropy measures the unpredictability an attacker must overcome, not just the length.

Why It Matters

Entropy measures the unpredictability of a random value — low-entropy secrets (short tokens, time-seeded randoms) are guessable; high-entropy values from a CSPRNG are not.

Common Mistakes

  • Using time-based seeds for token generation — timestamps are guessable within a small window.
  • Short tokens that reduce entropy even with a CSPRNG — 4 hex chars = 16 bits = 65,536 possibilities.
  • Not understanding that URL-safe encoding reduces entropy per character — use enough raw bytes.
  • Measuring entropy in characters rather than bits — 16 hex chars = only 64 bits of entropy.

Code Examples

✗ Vulnerable
// Low entropy token — guessable:
$token = dechex(time()); // Timestamp in hex — attacker knows approximate value
$token = substr(md5(rand()), 0, 8); // 8 hex chars = 32 bits — brute-forceable

// High entropy:
$token = bin2hex(random_bytes(32)); // 256 bits — not feasibly guessable
✓ Fixed
// High entropy = unpredictable; low entropy = guessable

// Low entropy — bad for secrets
\$token = mt_rand();          // 32-bit, predictable seed
\$token = time() . rand();    // ~40 bits, still predictable

// High entropy — cryptographically secure
\$token = random_bytes(32);   // 256 bits from OS CSPRNG (best)
\$hex   = bin2hex(\$token);    // 64-character hex string

// Measure password strength by entropy:
// 8-char lowercase only:  37 bits  — weak
// 12-char mixed case+digits+symbols: 78 bits — strong
// 4-word passphrase (diceware): ~51 bits — usable

// Entropy in UUIDs:
// UUID v4: 122 bits — suitable for public IDs
// UUID v7: 74 bits random + time-ordered — good for DB primary keys

Added 15 Mar 2026
Edited 22 Mar 2026
Views 33
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 2 pings S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S
Amazonbot 1
No pings yesterday
Amazonbot 14 Perplexity 4 Google 3 Ahrefs 3 ChatGPT 2 Majestic 1
crawler 25 crawler_json 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Use random_bytes(32) which draws from the OS entropy pool (getrandom on Linux, CryptGenRandom on Windows) — never use mt_rand or time-based seeds for anything security-sensitive
📦 Applies To
PHP 7.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
mt_srand(time()) seed based on time; weak token generation from unixtime; PHP rand() for cryptographic material
Auto-detectable: ✓ Yes semgrep psalm
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Line
CWE-330 CWE-338

✓ schema.org compliant