Node.js Crypto

The Node.js Crypto module bolsters cryptography. It gives cryptographic usefulness that incorporates a lot of wrappers for open SSL's hash HMAC, figure, unravel, sign and confirm functions.

What is Hash

A hash is a fixed-length series of bits for example procedurally and deterministically created from some subjective square of source data.

What is HMAC

HMAC represents Hash-based Message Authentication Code. It is a procedure for applying a hash calculation to the two information and a mystery key that outcomes in a solitary last hash.

Encryption Example utilizing Hash and HMAC

File: crypto_example1.js

  1. const crypto = require('crypto');  
  2. const secret = 'abcdefg';  
  3. const hash = crypto.createHmac('sha256', secret)  
  4.                    .update('Welcome to welookups')  
  5.                    .digest('hex');  
  6. console.log(hash);  

Open Node.js order brief and run the accompanying code:

  1. node crypto_example1.js  

Encryption example using Cipher

File: crypto_example2.js

  1. const crypto = require('crypto');  
  2. const cipher = crypto.createCipher('aes192''a password');  
  3. var encrypted = cipher.update('Hello welookups''utf8''hex');  
  4. encrypted += cipher.final('hex');  
  5. console.log(encrypted);   

Open Node.js command prompt and run the following code:

Decryption model utilizing Decipher

File: crypto_example3.js

  1. const crypto = require('crypto');  
  2. const decipher = crypto.createDecipher('aes192''a password');  
  3. var encrypted = '4ce3b761d58398aed30d5af898a0656a3174d9c7d7502e781e83cf6b9fb836d5';  
  4. var decrypted = decipher.update(encrypted, 'hex''utf8');  
  5. decrypted += decipher.final('utf8');  
  6. console.log(decrypted);  

welookups is optimized for learning.© welookups. 2018 - 2020 All Right Reserved and you agree to have read and accepted our term and condition.