node-firewalla
Version:
Package to talk your firewalla box & API
59 lines (58 loc) • 2.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto_1 = __importDefault(require("crypto"));
const fs_1 = __importDefault(require("fs"));
class SecureUtil {
constructor() {
if (SecureUtil._instance) {
return SecureUtil._instance;
}
SecureUtil._instance = this;
}
regenerateKeyPair() {
const { publicKey, privateKey } = crypto_1.default.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
});
this.importKeyPairFromString(publicKey, privateKey);
}
importKeyPairFromString(publicKey, privateKey) {
this.publicKey = publicKey;
this.privateKey = privateKey;
}
importKeyPair(publicKeyPath, privateKeyPath) {
this.publicKey = fs_1.default.readFileSync(publicKeyPath, { encoding: "utf-8" });
this.privateKey = fs_1.default.readFileSync(privateKeyPath, { encoding: "utf-8" });
}
rsaDecrypt(cipherBase64) {
const buffer = Buffer.from(cipherBase64, 'base64');
return crypto_1.default.privateDecrypt(this.privateKey, buffer).toString('utf8');
}
aesDecrypt(msg, key) {
let iv = Buffer.alloc(16);
iv.fill(0);
let bkey = Buffer.from(key.substring(0, 32), "utf8");
let decipher = crypto_1.default.createDecipheriv('aes-256-cbc', bkey, iv);
let plain = decipher.update(msg, 'base64', 'utf8');
return plain + decipher.final('utf8');
}
aesEncrypt(msg, key) {
let iv = Buffer.alloc(16);
iv.fill(0);
let bkey = Buffer.from(key.substring(0, 32), "utf8");
let cipher = crypto_1.default.createCipheriv('aes-256-cbc', bkey, iv);
let encrypted = cipher.update(msg, 'utf8', 'base64');
return encrypted += cipher.final('base64');
}
}
exports.default = new SecureUtil();