How Node really works · Chapter 22 of 34
Crypto
By the end of this chapter you can prove who sent a message, store passwords and secrets safely, and make values nobody can guess. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The system
- 6 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
The UPI provider is ready to POST to Tapri's webhook every time a customer pays. Kabir's question: anyone on the internet can POST to that URL too, so how does the server know "paid ₹240" really came from the provider? The same week Ravi wants staff logins on the counter tablets, and Anjali wants rider phone numbers stored so that a leaked backup does not leak the riders.
Plain JavaScript cannot do this safely: a checksum you invent can be recomputed by anyone, and a password stored as text is one leak away from every account. node:crypto, built on OpenSSL, does the hard parts.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
fingerprint: notice when the stock file changes
Write fingerprint(text). It returns the SHA-256 digest of text as 64 lowercase hex characters. The same text always gives the same digest, one changed character gives a different one, and text with characters such as ₹ is hashed as its UTF-8 bytes.
- Task 2 · interview question
Interview: safeEqual, compare secrets in constant time, not with ===
Write safeEqual(a, b), where a and b are strings or Buffers. It returns true when both hold exactly the same bytes (a string counts as its UTF-8 bytes), and false otherwise. Whenever the two have the same byte length, the comparison must be done by timingSafeEqual from node:crypto (the check watches for the call, for equal and for different values).
When the byte lengths differ (including '' against 'x'), it returns false and does not throw.
- Task 3
signWebhook and verifyWebhook: prove a payment came from the UPI provider
Write signWebhook(body, secret). It returns the HMAC-SHA256 of body with secret as the key, as 64 lowercase hex characters.
Write verifyWebhook(body, signature, secret). It returns true only when signature is exactly the signature of body with that secret, compared with your safeEqual, never with ===. It returns false (without throwing) for a changed body, a wrong secret, a signature with one character changed, a signature that is too short, and a plain SHA-256 of the body.
- Task 4
newOrderId and newApiToken: values nobody can guess
Write newOrderId(). It returns a new random version 4 UUID, in the usual lowercase form with dashes, different on every call.
Write newApiToken(bytes), with bytes 32 when it is left out. It returns that many random bytes as lowercase hex, so 2 times bytes characters: 64 by default, 32 for 16, 2 for 1. Every call gives a new token. Use node:crypto, never Math.random.
- Task 5
hashPassword and checkPassword: store staff passwords as salted scrypt hashes
Write hashPassword(password). It returns a promise of one string, saltHex:keyHex: a fresh random 16-byte salt (32 hex characters), a colon, and the 64-byte scrypt key of the password with that salt (128 hex characters), using scrypt's default cost settings. Hashing the same password twice gives two different strings, because the salt is new each time. Use the async scrypt, not scryptSync.
Write checkPassword(password, stored). It returns a promise of true when the password matches stored and false when it does not. It must accept any value made the same way (16-byte salt, 64-byte key, default settings), not only ones made by your hashPassword.
- Task 6
encrypt and decrypt: lock up rider phone numbers with AES-256-GCM
Write encrypt(text, key), where key is a 32-byte Buffer. It encrypts the UTF-8 text with AES-256-GCM and a fresh random 12-byte IV on every call, and returns { iv, tag, data }, all lowercase hex: iv is 24 characters, tag is the 16-byte auth tag (32 characters), data is the encrypted text. Encrypting the same text twice gives a different iv and different data.
Write decrypt(box, key). It returns the original text. It throws when the data or the tag was changed or the key is wrong, and it must read any box written by a standard AES-256-GCM cipher in the same hex layout.
What you will learn
- Hashes and HMACs: a fingerprint of data, and one only a key holder can make.
- timingSafeEqual: comparing secrets without leaking them.
- Random values and scrypt: unguessable ids, and slow password hashes.
- AES-256-GCM: encryption that notices tampering.
More in The system
Build "Crypto" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.