@pompeii-labs/cli
Version:
Magma CLI
36 lines (35 loc) • 1.28 kB
JavaScript
import os from "os";
import crypto from "crypto";
class Security {
static encrypt(data) {
const key = Security.getMachineKey();
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv("aes-256-gcm", Buffer.from(key, "hex"), iv);
const encrypted = Buffer.concat([
cipher.update(JSON.stringify(data), "utf8"),
cipher.final()
]);
const tag = cipher.getAuthTag();
return Buffer.concat([iv, tag, encrypted]).toString("base64");
}
static decrypt(encryptedData) {
const key = Security.getMachineKey();
const data = Buffer.from(encryptedData, "base64");
const iv = data.subarray(0, 16);
const tag = data.subarray(16, 32);
const encrypted = data.subarray(32);
const decipher = crypto.createDecipheriv("aes-256-gcm", Buffer.from(key, "hex"), iv);
decipher.setAuthTag(tag);
const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
return JSON.parse(decrypted.toString("utf8"));
}
static getMachineKey() {
const username = os.userInfo().username;
const hostname = os.hostname();
const homeDir = os.homedir();
return crypto.createHash("sha256").update(`${username}:${hostname}:${homeDir}:magma-secret`).digest("hex");
}
}
export {
Security
};