Monday, 14 May 2018

Nodejs Interview Questions and Answer 2 year Experience


Question: How to encrypt a value with SECRET KEY?
const {SHA256} = require("crypto-js");
var SECRET_KEY='secretkey';

var message='I am good.';
var hash=SHA256(message+SECRET_KEY).toString();
console.log(`Message: ${message}`);
console.log(`Hash: ${hash}`);

You need to install the crypto-js module.


Question: How to encrypt and decrypt the String?
const jwt = require('jsonwebtoken');
var SECRET_KEY='secretkey';

var data = {
  id: 10
};

var token = jwt.sign(data, SECRET_KEY);
console.log(token);//Give you long string 

var decoded = jwt.verify(token, SECRET_KEY);
console.log('decoded', decoded);