WeCodeAcademy

Node.js Buffers and Crypto

ExampleNode.js
const crypto = require("crypto");

const buf = Buffer.from("Hello");
console.log(buf);
console.log(buf.toString("hex"));
console.log(buf.toString("base64"));
console.log(Buffer.from("SGVsbG8=", "base64").toString());
console.log(buf.length);

console.log(crypto.createHash("sha256").update("hello").digest("hex"));
console.log(crypto.createHmac("sha256", "secret").update("data").digest("hex").length);
console.log(crypto.randomBytes(8).length);
console.log(typeof crypto.randomUUID());
Output
<Buffer 48 65 6c 6c 6f>
48656c6c6f
SGVsbG8=
Hello
5
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
64
8
string

A Buffer holds raw bytes. Hashing turns data into a fixed fingerprint: the same input always gives the same hash, which is how file checks work. Never store passwords with a plain hash: use a slow algorithm such as bcrypt or crypto.scrypt.

Quick check

What is a Buffer?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.