// @ts-check import { scryptSync, createCipheriv } from "crypto"; import { Buffer } from "buffer"; import grabKeys from "../../utils/grab-keys"; /** * # Encrypt String */ export default function encrypt({ data, encryptionKey, encryptionSalt, }) { var _a; if (!(data === null || data === void 0 ? void 0 : data.match(/./))) { console.log("Encryption string is invalid"); return data; } const { key: encrptKey, salt, keyLen, algorithm, bufferAllocSize, } = grabKeys({ encryptionKey }); if (!(encrptKey === null || encrptKey === void 0 ? void 0 : encrptKey.match(/.{8,}/))) { console.log("Encryption key is invalid"); return data; } if (!(salt === null || salt === void 0 ? void 0 : salt.match(/.{8,}/))) { console.log("Encryption salt is invalid"); return data; } const password = encrptKey; let key = scryptSync(password, salt, keyLen); let iv = Buffer.alloc(bufferAllocSize, 0); const cipher = createCipheriv(algorithm, key, iv); try { let encrypted = cipher.update(data, "utf8", "hex"); encrypted += cipher.final("hex"); return encrypted; } catch (error) { console.log("Error in encrypting =>", error.message); (_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Error Encrypting Data`, error); return data; } }