Constant-time comparison and secrets in memory

Compare secrets without leaking them through timing, in TypeScript, Python and Go, and hold secrets in memory in a way that limits where copies end up: buffers you can wipe, wrappers that refuse to print, and no secrets in core dumps or logs.

● MediumFREEBy ·Design problem·13 min read·Beginner → Intermediate
constant-time comparisontiming attacktimingSafeEqualhmac.compare_digestsecret handlingmemory hygiene

01Understand the problem

✓What you will learn
  • Explain how an early-exit comparison leaks a secret byte by byte
  • Use the right primitive in each language: timingSafeEqual, hmac.compare_digest, subtle.ConstantTimeCompare
  • Handle unequal lengths without throwing or leaking the secret length
  • Keep secrets in wipeable buffers behind a wrapper that will not print itself
◎Interview radar

What they probe: why === is a problem for tokens, what timingSafeEqual does with different lengths, whether hashing before comparing is sound, and what you can and cannot guarantee about wiping memory in a garbage-collected language. Where candidates slip: calling timingSafeEqual on strings of different lengths (it throws), comparing hex strings with a loop that returns early, promising that memory is fully erased in JavaScript or Go.

A normal string comparison stops at the first differing byte. If an attacker can submit guesses for an API key or MAC and measure response times precisely enough (statistics over many requests make even microsecond differences measurable on a quiet network), they learn how many leading bytes were right and can recover the secret one byte at a time: about 256 guesses per byte instead of 256 to the power of the length. A constant-time comparison touches every byte regardless of where the first difference is.

LanguagePrimitiveBehaviour on different lengths
Node.jscrypto.timingSafeEqual(a, b)Throws RangeError; compare equal-length digests instead
Pythonhmac.compare_digest(a, b)Returns False; may leak the lengths, not the contents
Gosubtle.ConstantTimeCompare(x, y)Returns 0 immediately; the length is not treated as secret
JavaMessageDigest.isEqual(a, b)Returns false; constant time (proportional to the first array) since Java 6u17

02Interfaces first

The contractts
/** Constant-time equality for secrets of any length. Never throws on a length mismatch. */
export interface SecretComparer {
  equals(given: string | Buffer, expected: string | Buffer): boolean;
}

/** A secret value that cannot be logged, stringified or serialised by accident. */
export interface SecretBox {
  /** Runs fn with the raw bytes; the buffer must not be kept after fn returns. */
  use<T>(fn: (bytes: Buffer) => T): T;
  /** Overwrites the bytes with zeros. Later use() calls throw. */
  wipe(): void;
}
iWhy the comparison takes both sides as input

A comparer that accepts (given, expected) can hash both to the same length before comparing. That removes the length mismatch problem and means the time depends only on the attacker's own input length, which they already know.

03Comparing safely in three languages

Node.jsts
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";

/** HMAC both values under a per-process random key: equal-length 32-byte outputs, and nothing to precompute. */
const COMPARE_KEY = randomBytes(32);
const digest = (v: string | Buffer) => createHmac("sha256", COMPARE_KEY).update(v).digest();

export const comparer: SecretComparer = {
  equals(given, expected) {
    return timingSafeEqual(digest(given), digest(expected));
  },
};

// What not to do:
//   given === expected                       early exit at the first differing character
//   timingSafeEqual(Buffer.from(given), Buffer.from(expected))   throws RangeError when lengths differ,
//                                            and the try/catch around it becomes a length oracle
Pythonpython
import hmac, hashlib

def verify_webhook(secret: bytes, body: bytes, signature_hex: str) -> bool:
    expected = hmac.new(secret, body, hashlib.sha256).hexdigest()
    # compare_digest accepts two str (ASCII only) or two bytes-like objects; never mix types.
    return hmac.compare_digest(expected, signature_hex.lower())
Gogo
package apikey

import (
	"crypto/sha256"
	"crypto/subtle"
)

// Equal hashes both sides so the lengths always match and the comparison is constant time in 32 bytes.
func Equal(given, expected []byte) bool {
	g := sha256.Sum256(given)
	e := sha256.Sum256(expected)
	return subtle.ConstantTimeCompare(g[:], e[:]) == 1
}

Storing API keys well removes most comparisons altogether. Issue keys as prefix_id_secret, store the id in clear and a SHA-256 of the full key (a fast hash is fine because the secret has 256 bits of entropy, unlike a password), look up by id, then compare the stored hash to the hash of what was presented in constant time. A leaked database row is then not a usable key.

A secret that refuses to printts
import { inspect } from "node:util";

export class Secret implements SecretBox {
  #bytes: Buffer | undefined;

  constructor(value: Buffer) {
    const b = Buffer.alloc(value.length);   // alloc, not allocUnsafe: never share a pooled slab with other data
    value.copy(b);
    this.#bytes = b;
  }
  static fromEnv(name: string): Secret {
    const v = process.env[name];
    if (!v) throw new Error("missing secret " + name);
    delete process.env[name];               // child processes no longer inherit it
    return new Secret(Buffer.from(v, "utf8"));
  }
  use<T>(fn: (bytes: Buffer) => T): T {
    if (!this.#bytes) throw new Error("secret already wiped");
    return fn(this.#bytes);
  }
  wipe(): void {
    this.#bytes?.fill(0);
    this.#bytes = undefined;
  }
  toString(): string { return "[REDACTED]"; }
  toJSON(): string { return "[REDACTED]"; }
  [inspect.custom](): string { return "Secret([REDACTED])"; }
}
▭On the whiteboard · Design choices4 points
01
compare
HMAC or hash both sides, then timingSafeEqual on equal-length digests
02
store
API keys as id plus SHA-256 of the key; passwords need a slow hash instead
03
hold
Buffer, not string: strings are immutable and cannot be wiped
04
print
toString, toJSON and inspect.custom all return a placeholder

04What memory hygiene can and cannot promise

RiskWhere copies come fromMitigation
Immutable stringsEvery +, template or decode makes a new copy you cannot eraseKeep secrets as Buffer or []byte from the moment they are read
Garbage collectionMoving collectors (V8, Go's stack growth) copy objects and leave old bytesAccept it; reduce lifetime and number of copies instead of promising erasure
Core dumpsA crash writes the whole heap to diskDisable dumps for secret-holding processes (ulimit -c 0, prctl(PR_SET_DUMPABLE, 0))
SwapPages with secrets are written to diskmlock in native code, encrypted swap, or no swap on those hosts
Logs and errorsAn exception message or debug dump includes the valueSecret wrapper; never put secrets into error messages
Environment variablesVisible in /proc/<pid>/environ and inherited by childrenRead once, remove from process.env, prefer a secrets manager or mounted file
!Wiping is best effort in managed runtimes

fill(0) really overwrites that buffer, but V8, the JVM and Go may already have copied the bytes elsewhere. Say this plainly in a review. For keys that must never be exposed to process memory at all, use an HSM or KMS that performs the operation for you.

vitestts
import { describe, it, expect } from "vitest";
import { inspect } from "node:util";

describe("comparer", () => {
  it("compares equal and unequal values of any length without throwing", () => {
    expect(comparer.equals("abc", "abc")).toBe(true);
    expect(comparer.equals("abc", "abcd")).toBe(false);
    expect(comparer.equals("", "x")).toBe(false);
  });
});

describe("Secret", () => {
  it("never prints its value", () => {
    const s = new Secret(Buffer.from("hunter2"));
    expect(String(s)).toBe("[REDACTED]");
    expect(JSON.stringify({ s })).toBe('{"s":"[REDACTED]"}');
    expect(inspect(s)).not.toContain("hunter2");
  });
  it("zeros its bytes and refuses use after wipe", () => {
    const s = new Secret(Buffer.from("k"));
    let seen: Buffer | undefined;
    s.use((b) => { seen = b; });
    s.wipe();
    expect(seen?.every((x) => x === 0)).toBe(true);
    expect(() => s.use(() => 1)).toThrow(/wiped/);
  });
});

05Extensions, follow-ups, how to say it

  • Beyond comparison: branches and table lookups indexed by secret data leak too; that is why crypto libraries use constant-time implementations and why you should not write your own.
  • Go helpers: subtle.ConstantTimeSelect and subtle.ConstantTimeByteEq exist for building constant-time code; crypto/subtle.XORBytes (Go 1.20) for XOR.
  • Remote timing in practice: jitter makes it harder, not impossible; statistical attacks average it out, so do not rely on network noise.
  • Rust and C: crates such as subtle and zeroize, or explicit_bzero and sodium_memzero in C, stop the compiler removing the wipe as a dead store.
Q1Is it safe to hash both values with SHA-256 and compare with `===`?+

Much safer than comparing the raw values, because an attacker cannot choose inputs that control hash prefixes. Still use a constant-time comparison: it costs nothing and removes the argument.

Q2timingSafeEqual threw RangeError in production. What happened?+

The two buffers had different lengths, typically a truncated or malformed token. Hash both to fixed length first, or check the format strictly (exact length regex) before comparing.

Q3Why does Go's ConstantTimeCompare return immediately on different lengths?+

It treats length as public. For tokens with a fixed format that is fine; for variable-length secrets, hash first.

“How to say it in the room

"For any secret comparison I bring both sides to equal-length digests, an HMAC under a random key or a SHA-256, then use the language's constant-time primitive: timingSafeEqual, compare_digest or ConstantTimeCompare. API keys are stored as an id plus a hash, so the database never holds a usable key. In memory I keep secrets as buffers behind a wrapper that redacts itself in logs and JSON, wipe them after use, keep them out of the environment and core dumps, and I am explicit that wiping is best effort in a garbage-collected runtime."

Test yourself

Which comparison is safe for checking a user-supplied HMAC signature in Node.js against the expected one?

Finished this one?
Tick it and it shows as done on the roadmap and the course.