Skip to content

Instantly share code, notes, and snippets.

@EarthenLynx
Last active July 22, 2020 13:35
Show Gist options
  • Save EarthenLynx/610838e71da3989c50a75e6f075a8c56 to your computer and use it in GitHub Desktop.
Save EarthenLynx/610838e71da3989c50a75e6f075a8c56 to your computer and use it in GitHub Desktop.
/**
* Helper function to encrypt a JSON or Javascript Object
*
*/
const crypto = require("crypto");
const jEncrypt = (pubKey, obj, callback) => {
const algorithm = "aes-192-cbc";
let privKey = crypto.scryptSync(pubKey, "salt", 24);
let iv = Buffer.alloc(16, 19);
let cipher = crypto.createCipheriv(algorithm, privKey, iv);
let save = cipher.update(obj, "utf8", "hex");
save += cipher.final("hex");
callback(save);
};
const jDecrypt = (pubKey, obj, callback) => {
let algorithm = "aes-192-cbc";
let privKey = crypto.scryptSync(pubKey, "salt", 24);
let iv = Buffer.alloc(16, 19);
let cipher = crypto.createDecipheriv(algorithm, privKey, iv);
let unsave = cipher.update(obj, "hex", "utf8");
unsave += cipher.final("utf8");
callback(unsave);
};
module.exports = { jEncrypt, jDecrypt };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment