Skip to content

Instantly share code, notes, and snippets.

@pdaug
Created July 18, 2023 18:26
Show Gist options
  • Save pdaug/a5889059391782dde57306b52e3f5212 to your computer and use it in GitHub Desktop.
Save pdaug/a5889059391782dde57306b52e3f5212 to your computer and use it in GitHub Desktop.
// Criptografar
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', CRYPTO_SECRET, iv);
let dadosCriptografados = cipher.update(JSON.stringify({imei, dataInicial, dataFinal}), 'utf8', 'hex');
dadosCriptografados += cipher.final('hex');
dadosCriptografados = iv.toString('hex') + dadosCriptografados;
// Descriptografar
const ivRecuperado = Buffer.from(dadosCriptografados.slice(0, 32), 'hex');
const dadosCriptografadosSemIV = dadosCriptografados.slice(32);
const decipher = crypto.createDecipheriv('aes-256-cbc', CRYPTO_SECRET, ivRecuperado);
let dadosDescriptografados = decipher.update(dadosCriptografadosSemIV, 'hex', 'utf8');
dadosDescriptografados += decipher.final('utf8');
dadosDescriptografados = JSON.parse(dadosDescriptografados);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment