Hashing and Crypto
The crypto module hashes data, signs messages with HMAC and makes random values. Hash passwords with a slow algorithm such as scrypt, not plain sha256.
Example
const crypto = require('node:crypto');
console.log(crypto.createHash('sha256').update('abc').digest('hex').slice(0, 16));
console.log(crypto.createHmac('sha256', 'key').update('msg').digest('hex').length);
console.log(crypto.randomUUID().length, crypto.randomBytes(8).length);
const salt = 'fixedsalt';
console.log(crypto.scryptSync('pw', salt, 16).toString('hex').length);Result
ba7816bf8f01cfea 64 36 8 32
Exercise
Create a hash object.
crypto.("sha256")
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.