Hash text in Node.js

aerial photography of city skyline
Reading Time: < 1 minutes

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');
      
    
,

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: