datasquirel/dist/package-shared/functions/dsql/decrypt.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

38 lines
1.5 KiB
JavaScript

// @ts-check
import { scryptSync, createDecipheriv } from "crypto";
import { Buffer } from "buffer";
import grabKeys from "../../utils/grab-keys";
/**
* # Decrypt Function
*/
export default function decrypt({ encryptedString, encryptionKey, encryptionSalt, }) {
var _a;
if (!(encryptedString === null || encryptedString === void 0 ? void 0 : encryptedString.match(/./))) {
console.log("Encrypted string is invalid");
return encryptedString;
}
const { key: encrptKey, salt, keyLen, algorithm, bufferAllocSize, } = grabKeys({ encryptionKey });
if (!(encrptKey === null || encrptKey === void 0 ? void 0 : encrptKey.match(/.{8,}/))) {
console.log("Decrption key is invalid");
return encryptedString;
}
if (!(salt === null || salt === void 0 ? void 0 : salt.match(/.{8,}/))) {
console.log("Decrption salt is invalid");
return encryptedString;
}
let key = scryptSync(encrptKey, salt, keyLen);
let iv = Buffer.alloc(bufferAllocSize, 0);
const decipher = 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);
console.log("encryptedString =>", encryptedString);
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Decrypting data`, error);
return encryptedString;
}
}