Sepia
0
Q:

node.js crypto.createcipheriv

const crypto = require('crypto');

const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
                   .update('I love cupcakes')
                   .digest('hex');
console.log(hash);
// Prints:
//   c0fa1bc00531bd78ef38c628449c5102aeabd49b5dc3a2a516ea6ea959d6658e
5
// Node.js program to demonstrate the      
// crypto.createCipheriv() method 
  
// Includes crypto module 
const crypto = require('crypto'); 
  
// Difining algorithm 
const algorithm = 'aes-256-cbc'; 
  
// Defining key 
const key = crypto.randomBytes(32); 
  
// Defining iv 
const iv = crypto.randomBytes(16); 
  
// An encrypt function 
function encrypt(text) { 
  
 // Creating Cipheriv with its parameter 
 let cipher = crypto.createCipheriv( 
      'aes-256-cbc', Buffer.from(key), iv); 
  
 // Updating text 
 let encrypted = cipher.update(text); 
  
 // Using concatenation 
 encrypted = Buffer.concat([encrypted, cipher.final()]); 
  
 // Returning iv and encrypted data 
 return { iv: iv.toString('hex'),  
    encryptedData: encrypted.toString('hex') }; 
} 
  
// Displays output 
var output = encrypt("GeeksforGeeks"); 
console.log(output); 
0

New to Communities?

Join the community