Crypto random
randomUUID
A UUID is a long random label with negligible collision odds. One call creates a text ID without counters or central coordination.
import { randomUUID } from "node:crypto"; const id: string = randomUUID(); console.log(id.length);
36
Every UUID is 36 characters long, dashes included; the value itself is random on each run.
randomBytes hex
Random bytes serve tokens of any chosen length. Hex encoding turns each byte into two readable characters, so size the byte count to half the wanted text length.
import { randomBytes } from "node:crypto"; const token: string = randomBytes(16).toString("hex"); console.log(token.length);
32
Timing-safe compare note
Plain equality can leak match length through response speed. The timing-safe helper compares full buffers in constant time for one length.
Only use it on same-length inputs, and hash secrets before storage. Treat this section as a concept note, not a full login system.
import { timingSafeEqual } from "node:crypto"; const a = Buffer.from("abc123"); const b = Buffer.from("abc123"); const same: boolean = a.length === b.length && timingSafeEqual(a, b); console.log(same);
true
IDs for records
Give each new record a fresh UUID at creation time. The ID then travels with the object through files, logs, and tests.
import { randomUUID } from "node:crypto"; const note = { id: randomUUID(), title: "Buy milk" }; console.log(note.id);
Never reuse IDs across records, and never derive them from titles. Random creation keeps references stable even after renames.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
randomUUID()return?Answer
A random UUID text of 36 characters
The call gives a random label with negligible collision odds, without coordination.
-
Why does hex encoding double the length of random bytes?
Answer
Each byte becomes two hex characters
One byte needs two hex digits, so 16 bytes make 32 characters.
-
What is
timingSafeEqualfor?Answer
Comparing two buffers of equal length in constant time
The helper avoids a timing leak, and it requires the two inputs to have the same length.
-
Why never derive a record ID from its title?
Answer
A rename would break every reference to the ID
A random ID stays stable when the title changes.
-
What does this program print?
import { randomBytes } from "node:crypto"; const token: string = randomBytes(16).toString("hex"); console.log(token.length);Answer
32
Sixteen bytes become thirty-two hex characters.
-
What does this program print?
import { timingSafeEqual } from "node:crypto"; const a = Buffer.from("abc123"); const b = Buffer.from("abc123"); const same: boolean = a.length === b.length && timingSafeEqual(a, b); console.log(same);Answer
true
The buffers hold the same bytes and have the same length, so the compare is true.
-
What does this program print?
console.log(Buffer.from("abc").length);Answer
3
The buffer holds one byte for each of the three characters.
-
What does this program print?
console.log("abc123".length);Answer
6
The text has six characters.
-
What does the compiler say about this program?
import { randomBytes } from "node:crypto"; const token: string = randomBytes(16); console.log(token);Answer
error TS2322: Type 'NonSharedBuffer' is not assignable to type 'string'.
randomBytes gives bytes; toString("hex") is what turns them into text.
-
What does the compiler say about this program?
import { timingSafeEqual } from "node:crypto"; const same: string = timingSafeEqual(Buffer.from("a"), Buffer.from("a")); console.log(same);Answer
error TS2322: Type 'boolean' is not assignable to type 'string'.
The compare returns a boolean, so a string variable cannot hold it.
Article author: Arthur Isaev