44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
"use strict";
|
|
// @ts-check
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = encrypt;
|
|
const crypto_1 = require("crypto");
|
|
const buffer_1 = require("buffer");
|
|
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
|
|
/**
|
|
* # Encrypt String
|
|
*/
|
|
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, } = (0, grab_keys_1.default)({ 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 = (0, crypto_1.scryptSync)(password, salt, keyLen);
|
|
let iv = buffer_1.Buffer.alloc(bufferAllocSize, 0);
|
|
const cipher = (0, crypto_1.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;
|
|
}
|
|
}
|