Skip to content

Instantly share code, notes, and snippets.

@EnetoJara
Created July 16, 2020 19:09
Show Gist options
  • Save EnetoJara/84bec82f88ef76685d856b1e96990dcc to your computer and use it in GitHub Desktop.
Save EnetoJara/84bec82f88ef76685d856b1e96990dcc to your computer and use it in GitHub Desktop.
const crypto = require("crypto");
const algorithm = "aes-256-cbc";
const iv = "%i!>Z+e"
const key = "7xFx-oli,Szlc%5D/k0&vq4b"
function scrypt (num, secret) {
return new Promise((res, rec) => {
return crypto.scrypt(secret, "salt", num, (err, key) => {
if (err) return rec(err);
return res(key);
});
});
}
function getIv () {
return scrypt(8, iv).then((response) => response.toString("hex"));
}
function getKey () {
return scrypt(32, key).then((response) => response);
}
function encryptText (toEncript) {
let i, k;
return getIv()
.then(iv => {
i = iv;
return getKey()
}).then(key => {
k = key;
const cipher = crypto.createCipheriv(algorithm, k, i);
const encrypted = cipher.update(toEncript, "utf8", "hex") + cipher.final("hex");
return encrypted;
});
}
function decryptText (toDencript) {
let i, k;
return getIv()
.then((res1) => {
i = res1;
return getKey()
})
.then((ae) => {
k = ae;
const decipher = crypto.createDecipheriv(algorithm, k, i);
const encrypted = decipher.update(toDencript, "hex", "utf8") + decipher.final("utf8");
return encrypted;
})
.catch((err) => {
console.log("err: ", err);
throw err;
});
}
(async function () {
const textToEncript = "enetito is very pretty to them girls troll mon troll mon long text";
console.log('textToEncript: ', textToEncript);
const encrypted = await encryptText(textToEncript);
console.log('encrypted: ', encrypted);
const textDecripted = await decryptText(encrypted)
console.log('textDecripted: ', textDecripted);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment