3d7ff76e8f87ed83011d90bbafedd08c5b96904b
[motion2.git] / server / components / encryptor / index.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x9475=['decryptFile','createDecipher','close','encryptString','base64','secret','utf8','crypto','bluebird','lodash','../../config/environment','../../config/logger','app','secrets','session','aes192','encrypt','algorithm','join','error','test','getCiphers','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','data','update','write','end','final','binary','unlink'];(function(_0x4d1c64,_0x1e1d66){var _0x5f403d=function(_0x46a11d){while(--_0x46a11d){_0x4d1c64['push'](_0x4d1c64['shift']());}};_0x5f403d(++_0x1e1d66);}(_0x9475,0xfc));var _0x5947=function(_0x3f32ac,_0x12686f){_0x3f32ac=_0x3f32ac-0x0;var _0x581573=_0x9475[_0x3f32ac];return _0x581573;};'use strict';var crypto=require(_0x5947('0x0'));var fs=require('fs');var BPromise=require(_0x5947('0x1'));var _=require(_0x5947('0x2'));var config=require(_0x5947('0x3'));var logger=require(_0x5947('0x4'))(_0x5947('0x5'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x5947('0x6')][_0x5947('0x7')],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0x5947('0x8')};if(config[_0x5947('0x9')]&&config[_0x5947('0x9')][_0x5947('0xa')]){var isSupported=isAlgorithmSupported(config[_0x5947('0x9')][_0x5947('0xa')]);if(isSupported){fileOptions['algorithm']=config[_0x5947('0x9')][_0x5947('0xa')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x5947('0xb')](',\x20');logger[_0x5947('0xc')](config[_0x5947('0x9')][_0x5947('0xa')],'is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:',supportedAlgorithms);}}function isASupportedCipher(_0x2d4397){return AES_SUPPORTED_RX[_0x5947('0xd')](_0x2d4397);}function getSupportedCiphers(){return crypto[_0x5947('0xe')]()['filter'](isASupportedCipher);}function isAlgorithmSupported(_0x28a103){return getSupportedCiphers()[_0x5947('0xf')](_0x28a103)>-0x1;}function combineOptions(_0x310df8){var _0x84c343={};for(var _0x49820f in fileOptions){if(fileOptions['hasOwnProperty'](_0x49820f)){_0x84c343[_0x49820f]=fileOptions[_0x49820f];}}if(_0x310df8){for(var _0x475037 in _0x310df8){if(_0x310df8[_0x5947('0x10')](_0x475037)){_0x84c343[_0x475037]=_0x310df8[_0x475037];}}}return _0x84c343;}exports[_0x5947('0x11')]=function(_0x2fd2e8,_0x432b32,_0x381ad9,_0x18a394){return new BPromise(function(_0xf4d544,_0x302604){_0x18a394=combineOptions(_0x18a394);var _0x2346b5=new Buffer(_0x381ad9);var _0x5e5255=fs[_0x5947('0x12')](_0x2fd2e8);var _0x2d8086=fs[_0x5947('0x13')](_0x432b32);var _0x234ea0=crypto[_0x5947('0x14')](_0x18a394[_0x5947('0xa')],_0x2346b5);_0x5e5255['on'](_0x5947('0x15'),function(_0x4213f8){var _0x184964=new Buffer(_0x234ea0[_0x5947('0x16')](_0x4213f8),'binary');_0x2d8086[_0x5947('0x17')](_0x184964);});_0x5e5255['on'](_0x5947('0x18'),function(){try{var _0x40f148=new Buffer(_0x234ea0[_0x5947('0x19')](_0x5947('0x1a')),'binary');_0x2d8086['write'](_0x40f148);_0x2d8086[_0x5947('0x18')]();_0x2d8086['on']('close',function(){return _0xf4d544(_0x432b32);});}catch(_0x213054){fs[_0x5947('0x1b')](_0x432b32);return _0x302604(_0x213054);}});});};exports[_0x5947('0x1c')]=function(_0x46a9dc,_0x5cef3e,_0x566495,_0x2d513b){return new BPromise(function(_0x238c9f,_0x9caa33){_0x2d513b=combineOptions(_0x2d513b);var _0x2ca252=new Buffer(_0x566495);var _0x4d3478=fs['createReadStream'](_0x46a9dc);var _0x2f1d12=fs[_0x5947('0x13')](_0x5cef3e);var _0x1cfb34=crypto[_0x5947('0x1d')](_0x2d513b[_0x5947('0xa')],_0x2ca252);_0x4d3478['on'](_0x5947('0x15'),function(_0x4fd677){var _0x35215f=new Buffer(_0x1cfb34[_0x5947('0x16')](_0x4fd677),'binary');_0x2f1d12[_0x5947('0x17')](_0x35215f);});_0x4d3478['on'](_0x5947('0x18'),function(){try{var _0x484d07=new Buffer(_0x1cfb34[_0x5947('0x19')]('binary'),_0x5947('0x1a'));_0x2f1d12[_0x5947('0x17')](_0x484d07);_0x2f1d12[_0x5947('0x18')]();_0x2f1d12['on'](_0x5947('0x1e'),function(){return _0x238c9f(_0x5cef3e);});}catch(_0x5760e1){fs[_0x5947('0x1b')](_0x5cef3e);return _0x9caa33(_0x5760e1);}});});};exports[_0x5947('0x1f')]=function(_0x473e80,_0x58da8d,_0x2c43d9){var _0x3c36c0=_0x2c43d9||stringOptions[_0x5947('0xa')];var _0x3d5c16=_0x58da8d||stringOptions['secret'];var _0xb484d3=crypto[_0x5947('0x14')](_0x3c36c0,_0x3d5c16);var _0x32f047=_0xb484d3['update'](_0x473e80,'utf8',_0x5947('0x20'));_0x32f047+=_0xb484d3['final'](_0x5947('0x20'));return _0x32f047;};exports['decryptString']=function(_0x732ab4,_0x23e052,_0x548991){var _0x5d3c8b=_0x548991||stringOptions[_0x5947('0xa')];var _0x4a98d3=_0x23e052||stringOptions[_0x5947('0x21')];var _0x288f9b=crypto[_0x5947('0x1d')](_0x5d3c8b,_0x4a98d3);var _0x5080fc=_0x288f9b['update'](_0x732ab4,'base64',_0x5947('0x22'));_0x5080fc+=_0x288f9b[_0x5947('0x19')]('utf8');return _0x5080fc;};