advanced

Web Crypto

Use secure random values, hashing, signatures, key import/export, and encryption primitives without inventing crypto.

Web Crypto exposes browser-provided cryptographic primitives such as secure random generation, hashing, signatures, key derivation, and encryption. It is asynchronous and safer than ad hoc JavaScript crypto, but correct protocol design is still hard. Interview answers should avoid inventing schemes and focus on primitives, keys, and threat model.

					const bytes = crypto.getRandomValues(new Uint8Array(16));
const digest = await crypto.subtle.digest('SHA-256', data);
				

Hashing is not encryption. Non-extractable keys reduce XSS exfiltration risk but cannot protect against a fully compromised client. Password handling belongs on the server with established KDFs.

On interviews: random IDs, hashing versus encryption, key extractability, and why passwords should not be handled casually client-side.

Common pitfalls: crypto APIs do not make insecure protocols safe. Client-side keys can be exposed by XSS or compromised devices.

The trade-off is convenience versus control — pick the mechanism that matches your coupling and performance budget.

Checklist:

  • Use getRandomValues for IDs and tokens.
  • Know hash vs encrypt vs sign.
  • Prefer non-extractable keys when possible.
  • Do not design custom crypto protocols.