Created
July 18, 2023 18:26
-
-
Save pdaug/a5889059391782dde57306b52e3f5212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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