3. Hash API keys with SHA-256, not bcrypt
- Status: Accepted
- Date: 2026-07-13
Context
API keys must not be stored in plaintext. The reflexive answer is bcrypt — it's what you use for passwords, and a security checklist that says "passwords are bcrypted" is a checklist most people generalise to "secrets are bcrypted."
The project's own early SECURITY.md claimed bcrypt. The code used SHA-256. One of them had to change, and it's worth writing down which and why, because the reasoning is not obvious and "we used the fast hash" reads like a corner being cut.
Decision
Generate keys with secrets.token_urlsafe(32) and store sha256(key). Do not use bcrypt.
Rationale
Bcrypt is slow on purpose, and that purpose does not apply here.
Bcrypt's cost factor exists because passwords are low-entropy. Humans pick hunter2, and
an attacker with a stolen hash database runs a dictionary against it. Making each guess
cost 100ms is what turns a feasible attack into an infeasible one.
An API key here is 32 bytes from a CSPRNG — 256 bits of entropy, uniformly distributed. There is no dictionary. There is no "likely" key to guess first. Brute-forcing it is infeasible against a hash that runs in nanoseconds, and remains exactly as infeasible against one that takes 100ms. The slowness buys nothing, because the thing it defends against isn't on the table.
Meanwhile the slowness costs a great deal. The key is verified by database lookup on every single request. Bcrypt at a sensible cost factor would add ~100ms of pure CPU to the hot path of every call — and, worse, would hand any unauthenticated client a trivial denial-of-service: spray invalid keys and watch the gateway burn its CPU hashing them.
Fast hashing of a high-entropy secret is the correct construction. Slow hashing of a low-entropy one is the case bcrypt exists for. Applying the password tool to the API-key problem is a category error that happens to look responsible.
This is the same reasoning behind how Stripe, GitHub and AWS handle API keys.
Consequences
- Key verification is a single indexed lookup on a SHA-256 hex digest. No CPU cost, no DoS surface.
- Keys must be generated by localgate, never chosen by a user. The whole argument rests
on the key being high-entropy; a user-supplied key could be
password123, at which point the fast hash genuinely is a weakness.generate_key()is the only way a key is created, and there is no endpoint that accepts one. - The raw key is shown exactly once and is not recoverable.
key_prefix(the first 11 characters) is stored in plaintext so a key can be identified in a listing — otherwise there is no way to tell which row corresponds to the key in your hand. - The admin key is compared with
hmac.compare_digest, not==. String comparison short-circuits at the first differing byte, so the time it takes to fail leaks how much of the secret the caller got right — enough, over many requests, to reconstruct it a byte at a time. That one is a real attack, and it is defended against. - SECURITY.md now says SHA-256 and explains why, rather than claiming bcrypt.