react-genx-cli
Version:
51 lines (44 loc) • 1.64 kB
JavaScript
const crypto = require("crypto");
function encrypt(apiKey, password) {
const salt = crypto.randomBytes(16);
const iv = crypto.randomBytes(12);
const key = crypto.pbkdf2Sync(password, salt, 100000, 32, "sha256");
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
let encrypted = cipher.update(apiKey, "utf8", "hex");
encrypted += cipher.final("hex");
const authTag = cipher.getAuthTag();
console.log(`this.encryptedKey = {
data: "${encrypted}",
salt: "${salt.toString("hex")}",
iv: "${iv.toString("hex")}",
authTag: "${authTag.toString("hex")}"
};`);
}
const key =
encrypt(key, "iamneo");
// function decryptKey(password) {
// const { data, salt, iv, authTag } = {
// data: "b714c2e70da8739e881c57e7142517750bc80ad1116b052c1b146ac08902fdebc7938031e653f03dd5df68a07de5575e52f23ef009cf91c8f056368c16d6404578c242f8e016a6d34aaefa201900a76ffb470f57117e7703681f1381dfb96723f2e8eec07061b91a05515db7",
// salt: "4bafb6f24952d6c1d84aa5d2f3f02f28",
// iv: "2a2cebb934f88e083455e467",
// authTag: "91024146132af26919b78e71503e101e",
// };
// const key = crypto.pbkdf2Sync(
// password,
// Buffer.from(salt, "hex"),
// 100000,
// 32,
// "sha256"
// );
// const decipher = crypto.createDecipheriv(
// "aes-256-gcm",
// key,
// Buffer.from(iv, "hex")
// );
// decipher.setAuthTag(Buffer.from(authTag, "hex"));
// let decrypted = decipher.update(data, "hex", "utf8");
// decrypted += decipher.final("utf8");
// console.log(decrypted);
// return decrypted;
// }
// decryptKey("iamneo");