44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
"use strict";
|
|
// @ts-check
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = encrypt;
|
|
const crypto_1 = require("crypto");
|
|
const buffer_1 = require("buffer");
|
|
/**
|
|
* # Encrypt String
|
|
*/
|
|
function encrypt({ data, encryptionKey, encryptionSalt, }) {
|
|
if (!(data === null || data === void 0 ? void 0 : data.match(/./))) {
|
|
console.log("Encryption string is invalid");
|
|
return data;
|
|
}
|
|
const finalEncryptionKey = encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
|
const finalEncryptionSalt = encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
|
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
|
|
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
|
|
: 24;
|
|
if (!(finalEncryptionKey === null || finalEncryptionKey === void 0 ? void 0 : finalEncryptionKey.match(/.{8,}/))) {
|
|
console.log("Encryption key is invalid");
|
|
return data;
|
|
}
|
|
if (!(finalEncryptionSalt === null || finalEncryptionSalt === void 0 ? void 0 : finalEncryptionSalt.match(/.{8,}/))) {
|
|
console.log("Encryption salt is invalid");
|
|
return data;
|
|
}
|
|
const algorithm = "aes-192-cbc";
|
|
const password = finalEncryptionKey;
|
|
let key = (0, crypto_1.scryptSync)(password, finalEncryptionSalt, finalKeyLen);
|
|
let iv = buffer_1.Buffer.alloc(16, 0);
|
|
// @ts-ignore
|
|
const cipher = (0, crypto_1.createCipheriv)(algorithm, key, iv);
|
|
try {
|
|
let encrypted = cipher.update(data, "utf8", "hex");
|
|
encrypted += cipher.final("hex");
|
|
return encrypted;
|
|
}
|
|
catch ( /** @type {*} */error) {
|
|
console.log("Error in encrypting =>", error.message);
|
|
return data;
|
|
}
|
|
}
|