Last active
July 22, 2020 13:35
-
-
Save EarthenLynx/610838e71da3989c50a75e6f075a8c56 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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