← Math
Math

Entropy and bits of strength

How strong is this API key, really?

Estimate guessing strength from the random choices a generator actually makes.

Outcome

Turn alphabet size and length into bits, count only random choices, compare with a guess budget, and catch bias in a generator.

Terminology (6)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Bits of entropy
log₂ of the number of equally likely values a generator can produce. Each additional bit doubles this uniform choice space.
Alphabet size
How many symbols each character can be: 10 for digits, 62 for A–Z, a–z and 0–9.
Min-entropy
−log₂ of the probability of the most likely value. It measures the attacker’s best single guess, and it is lower than Shannon entropy when the values are not equally likely.
Modulo bias
Reducing a random byte with % n when n does not divide 256 makes some symbols more likely than others.
Guess budget
How many guesses an attacker can make, for example a rate times a time window. Its log₂ is the bits it can cover.
CSPRNG
A cryptographically secure random number generator, such as crypto.getRandomValues or Go’s crypto/rand. Entropy calculations assume one.

Why this matters

Session IDs, API keys, invite codes and reset tokens are safe only if an attacker cannot guess them. Bits of entropy turn a format into a number you can compare with a policy and with an attacker’s guess budget. The number describes the assumed generator, so the review has to follow how each character was produced, not how long the key looks.

This lesson assumes logarithms and powers of two from earlier lessons.

What bits of strength count

Bits express the uncertainty in a key on a base-two scale. Counting independent, equally likely choices gives the size of the uniform search space. Public text and values derived from the random body do not add independent choices.

Unequal probabilities require a different measure. This review uses min-entropy, defined by the probability of the most likely value, to describe the attacker’s best single guess. Its strength claims depend on the assumed generator and independence of its draws; the later guess-budget calculation makes those assumptions explicit.

01Count choices in bits

Count choices in bits

A proposed key looks like sk_live_ followed by 28 letters and digits. Its length suggests over 200 bits, but the prefix is public, the last four characters are a checksum, and the body comes from random bytes reduced with %. The review needs the bits an attacker actually faces.

A character chosen uniformly from 62 symbols is one of 62 equally likely values, which takes log₂ 62 ≈ 5.95 binary choices to pin down. Independent characters multiply the possibilities, so their bits add. Predict what this program prints for a 24-character base62 body.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const length = 24; // characters in the key body
const alphabet = 62; // A–Z, a–z, 0–9

console.log((length * Math.log(alphabet)).toFixed(2)); // natural log
console.log((length * Math.log2(alphabet)).toFixed(2)); // uniform choices
console.log((length * -Math.log2(Math.ceil(256 / alphabet) / 256)).toFixed(2)); // byte % 62
Which line is the key’s entropy if every body character is chosen uniformly?

Math.log is the natural logarithm; Math.log2 gives bits. crypto.getRandomValues fills a typed array with cryptographically random bytes, in browsers and in Node.js.

02Only random choices count

Only random choices count

The full key is sk_live_, then the 24-character body, then a 4-character checksum. The prefix is the same for every key and the checksum is computed from the body, so an attacker who guesses the body knows the whole key.

How many bits does the 36-character key carry?

03Bits against a guess budget

Bits against a guess budget

Assume one target key with a uniform body. An attacker making 10⁹ distinct guesses a second for a year makes about 3.15 × 10¹⁶ guesses, which is log₂ of that, 54.81 bits. Against 142.90 bits, the chance of hitting a particular key is the guess count divided by the possible bodies, approximately 2^(54.81 − 142.90) = 2⁻⁸⁸·⁰⁹. The same arithmetic shows why a 6-digit PIN needs a lockout: 19.93 bits fall to a few thousand guesses a second within minutes.

Bits of common formats, assuming a uniform CSPRNG
FormatRandom charactersBits
6-digit PIN6 from 1019.93
Invite code16 from 3280.00
UUID version 4122 random bits; 6 bits fixed122.00
sk_live_ key24 from 62142.90

04Modulo bias

Modulo bias

The proposed base62 generator takes an independent uniform byte and keeps byte % 62. Since 256 = 4 × 62 + 8, eight symbols receive 5 byte values and the other 54 receive 4. Each common symbol has probability 5/256; the alphabet is not uniform.

How many bits of min-entropy does each character carry?

This review uses min-entropy because it measures the attacker’s best single guess. For 24 independent characters, the most likely whole body has probability (5/256)²⁴, so its min-entropy is 136.27 bits rather than the uniform 142.90. For g guesses, g × 2^(−min-entropy) is an upper bound on success, capped at 1; it is not a complete description of the best multi-guess strategy. When the alphabet size divides 256, as 16, 32 and 64 do, % is uniform. Otherwise, discard bytes at or above the last full multiple of the alphabet size and draw again. For base62, accept bytes below 248: all 62 symbols then receive exactly four byte values.

05Decide on the unrounded total

Decide on the unrounded total

06Review the key format

Review the key format

The format review first needs the right logarithm for its uniform body. It then needs to follow dependencies: a visible prefix and a derived checksum add length without adding choices. Finally, inspect how each random character is generated; the most likely modulo-produced body and the unrounded threshold comparison determine whether the format meets the stated requirement.

In the frontend A browser invite code in Next.js and Svelte Optional

Math.random is not a CSPRNG, so generate codes with crypto.getRandomValues. The helper drops bytes past the last full multiple of the alphabet size, so every symbol stays equally likely for a supported replacement alphabet of 1–128 distinct ASCII symbols. Sixteen characters from 32 symbols give 80 modeled bits, assuming independent uniform draws. The CSPRNG API is the source used by this example; the count does not measure its internal entropy.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
"use client";

import { useState } from "react";

const ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; // 32 symbols, 5 bits each

// Drop bytes at or above the last full multiple of the alphabet size, so every symbol is equally likely.
// With 32 symbols nothing is dropped; replacement alphabets must contain 1–128 distinct ASCII symbols.
function inviteCode(length: number): string {
  if (!Number.isInteger(length) || length < 0 || length > 1_024) {
    throw new RangeError("Length must be a whole number from 0 to 1,024");
  }

  if (
    !/^[\x00-\x7F]{1,128}$/.test(ALPHABET) ||
    new Set(ALPHABET).size !== ALPHABET.length
  ) {
    throw new RangeError("Use 1–128 distinct ASCII symbols");
  }

  const limit = 256 - (256 % ALPHABET.length);
  let code = "";

  while (code.length < length) {
    for (const byte of crypto.getRandomValues(new Uint8Array(length))) {
      if (byte < limit && code.length < length)
        code += ALPHABET[byte % ALPHABET.length];
    }
  }

  return code;
}

export function InviteButton() {
  const [code, setCode] = useState<string | null>(null);

  return (
    <p>
      <button onClick={() => setCode(inviteCode(16))}>New invite code</button>{" "}
      {/* 80 bits */}
      {code && <code>{code}</code>}
    </p>
  );
}

Check this idea later
How many random bits does a version 4 UUID carry?
A user picks a 12-character password from 94 printable characters. Does it have 12 × log₂ 94 ≈ 78.7 bits?
Sources

Values above were computed in Node 22 and checked in Go 1.27, Rust 1.99 and Apple clang 17. The key review is a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.