datasquirel/dist/package-shared/functions/dsql/decrypt.js
Benjamin Toby a3561da53d Updates
2025-01-10 20:35:05 +01:00

42 lines
1.7 KiB
JavaScript

"use strict";
// @ts-check
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = decrypt;
const crypto_1 = require("crypto");
const buffer_1 = require("buffer");
/**
* # Decrypt Function
*/
function decrypt({ encryptedString, encryptionKey, encryptionSalt, }) {
if (!(encryptedString === null || encryptedString === void 0 ? void 0 : encryptedString.match(/./))) {
console.log("Encrypted string is invalid");
return encryptedString;
}
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("Decrption key is invalid");
return encryptedString;
}
if (!(finalEncryptionSalt === null || finalEncryptionSalt === void 0 ? void 0 : finalEncryptionSalt.match(/.{8,}/))) {
console.log("Decrption salt is invalid");
return encryptedString;
}
const algorithm = "aes-192-cbc";
let key = (0, crypto_1.scryptSync)(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
let iv = buffer_1.Buffer.alloc(16, 0);
const decipher = (0, crypto_1.createDecipheriv)(algorithm, key, iv);
try {
let decrypted = decipher.update(encryptedString, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
}
catch (error) {
console.log("Error in decrypting =>", error.message);
return encryptedString;
}
}