Hashing is an essential part of encryption and data security. A hash is an algorithm that takes the text of a file, message, or other data and converts it into a unique string of characters. The hash acts as an immutable representation of the text and can be used to verify the integrity of the original data.
In this post, we’ll look at how to hash text in Node.js with the Crypto library.
const crypto = require('crypto');
const stringToHash = 'hello';
const hash = crypto.createHash('sha256').update(stringToHash).digest('hex');
// the output will be something like: 20ccfac05d596dfb08509494fc7753780ac6d79a23c88ef02514f4c5b2a3ff45
// instead of .digest('hex'); you can use also .digest('base64');