alapa
Version: 
A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.
44 lines (43 loc) • 1.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Encryption = void 0;
const crypto_1 = __importDefault(require("crypto"));
const utils_1 = require("../../utils");
const algorithm = "aes-256-cbc";
const defaultKey = "eccf8969b46e20938484029576643fccdcdcfdd2c7df272d5a62e0";
const secretKey = (0, utils_1.md5)(process.env.ENCRYPTION_KEY || defaultKey);
const iv = crypto_1.default.randomBytes(16);
class Encryption {
    static encrypt = (text) => {
        try {
            const cipher = crypto_1.default.createCipheriv(algorithm, Buffer.from(secretKey), iv);
            let encrypted = cipher.update(text);
            encrypted = Buffer.concat([encrypted, cipher.final()]);
            return iv.toString("hex") + ":" + encrypted.toString("hex");
        }
        catch (e) {
            console.log(e);
            return null;
        }
    };
    static decrypt = (text) => {
        try {
            const textParts = text.split(":");
            const iv = Buffer.from(textParts.shift(), "hex");
            const encryptedText = Buffer.from(textParts.join(":"), "hex");
            const decipher = crypto_1.default.createDecipheriv(algorithm, Buffer.from(secretKey), iv);
            let decrypted = decipher.update(encryptedText);
            decrypted = Buffer.concat([decrypted, decipher.final()]);
            return decrypted.toString();
            // eslint-disable-next-line @typescript-eslint/no-unused-vars
        }
        catch (e) {
            //console.log(e);
            return null;
        }
    };
}
exports.Encryption = Encryption;