Encriptando datos con node js

Encriptando datos con node js

Crear aplicación

mkdir dummy && cd dummy

npm init -y

Instalar dependencias

npm i crypto-js

npm i dotenv

Crear .env

SECRET_KEY = AABBCC112233

Crear utils/encription.js

require('dotenv').config()
const CryptoJS = require('crypto-js')

const secretKey = process.env.SECRET_KEY

const encrypt = (phrase) => {
  const res = CryptoJS.AES.encrypt(phrase, secretKey).toString()
  return res
}

const decrypt = (hash) => {
  const res = CryptoJS.AES.decrypt(hash, secretKey).toString(CryptoJS.enc.Utf8)
  return res
}

module.exports = {
  encrypt: encrypt,
  decrypt: decrypt,
}

Crear index.js

const { encrypt, decrypt } = require('./utils/encryption')

const encrypted = encrypt('admin$11')

console.log(encrypted)

const decrypted = decrypt(encrypted)

console.log(decrypted)

Ejecutar aplicación

node index.js