c39df15644c7155dbab3afc3443b6bb6c1397662
[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 _0xe73c=['unlink','createDecipher','update','encryptString','secret','base64','decryptString','utf8','crypto','bluebird','lodash','../../config/environment','../../config/logger','secrets','session','aes-256-ctr','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','filter','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','data','binary','write','end','final','close'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0xe73c,0xda));var _0xce73=function(_0x29c308,_0x4c24a6){_0x29c308=_0x29c308-0x0;var _0xd11e59=_0xe73c[_0x29c308];return _0xd11e59;};'use strict';var crypto=require(_0xce73('0x0'));var fs=require('fs');var BPromise=require(_0xce73('0x1'));var _=require(_0xce73('0x2'));var config=require(_0xce73('0x3'));var logger=require(_0xce73('0x4'))('app');const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xce73('0x5')][_0xce73('0x6')],'algorithm':_0xce73('0x7')};const fileOptions={'algorithm':_0xce73('0x8')};if(config[_0xce73('0x9')]&&config['encrypt'][_0xce73('0xa')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0xce73('0xa')]);if(isSupported){fileOptions[_0xce73('0xa')]=config[_0xce73('0x9')][_0xce73('0xa')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xce73('0xb')](',\x20');logger[_0xce73('0xc')](config[_0xce73('0x9')][_0xce73('0xa')],_0xce73('0xd'),supportedAlgorithms);}}function isASupportedCipher(_0x4dab11){return AES_SUPPORTED_RX['test'](_0x4dab11);}function getSupportedCiphers(){return crypto['getCiphers']()[_0xce73('0xe')](isASupportedCipher);}function isAlgorithmSupported(_0x475703){return getSupportedCiphers()[_0xce73('0xf')](_0x475703)>-0x1;}function combineOptions(_0x428f22){var _0x19c0f9={};for(var _0x2802aa in fileOptions){if(fileOptions[_0xce73('0x10')](_0x2802aa)){_0x19c0f9[_0x2802aa]=fileOptions[_0x2802aa];}}if(_0x428f22){for(var _0x569798 in _0x428f22){if(_0x428f22[_0xce73('0x10')](_0x569798)){_0x19c0f9[_0x569798]=_0x428f22[_0x569798];}}}return _0x19c0f9;}exports[_0xce73('0x11')]=function(_0x434e5a,_0x33059a,_0x29d3a5,_0x22182e){return new BPromise(function(_0x4a92ca,_0x2c8b88){_0x22182e=combineOptions(_0x22182e);var _0x243d68=new Buffer(_0x29d3a5);var _0x4f64f8=fs[_0xce73('0x12')](_0x434e5a);var _0x29e956=fs[_0xce73('0x13')](_0x33059a);var _0xf6864e=crypto[_0xce73('0x14')](_0x22182e['algorithm'],_0x243d68);_0x4f64f8['on'](_0xce73('0x15'),function(_0x4edf6e){var _0x244601=new Buffer(_0xf6864e['update'](_0x4edf6e),_0xce73('0x16'));_0x29e956[_0xce73('0x17')](_0x244601);});_0x4f64f8['on'](_0xce73('0x18'),function(){try{var _0xda3a80=new Buffer(_0xf6864e[_0xce73('0x19')]('binary'),_0xce73('0x16'));_0x29e956[_0xce73('0x17')](_0xda3a80);_0x29e956[_0xce73('0x18')]();_0x29e956['on'](_0xce73('0x1a'),function(){return _0x4a92ca(_0x33059a);});}catch(_0x24e650){fs[_0xce73('0x1b')](_0x33059a);return _0x2c8b88(_0x24e650);}});});};exports['decryptFile']=function(_0x800e38,_0x389136,_0x32c2eb,_0x2efdff){return new BPromise(function(_0x46dd98,_0x518930){_0x2efdff=combineOptions(_0x2efdff);var _0x1ddb61=new Buffer(_0x32c2eb);var _0xfbc2f7=fs[_0xce73('0x12')](_0x800e38);var _0x317637=fs['createWriteStream'](_0x389136);var _0x3e33d1=crypto[_0xce73('0x1c')](_0x2efdff[_0xce73('0xa')],_0x1ddb61);_0xfbc2f7['on'](_0xce73('0x15'),function(_0x30eb0d){var _0x9d10f7=new Buffer(_0x3e33d1[_0xce73('0x1d')](_0x30eb0d),_0xce73('0x16'));_0x317637[_0xce73('0x17')](_0x9d10f7);});_0xfbc2f7['on']('end',function(){try{var _0x4f15d2=new Buffer(_0x3e33d1[_0xce73('0x19')](_0xce73('0x16')),_0xce73('0x16'));_0x317637['write'](_0x4f15d2);_0x317637['end']();_0x317637['on']('close',function(){return _0x46dd98(_0x389136);});}catch(_0x4a038d){fs['unlink'](_0x389136);return _0x518930(_0x4a038d);}});});};exports[_0xce73('0x1e')]=function(_0x5a20c4,_0x405bbb,_0x36f81a){var _0x5d9ddc=_0x36f81a||stringOptions[_0xce73('0xa')];var _0x17aeb7=_0x405bbb||stringOptions[_0xce73('0x1f')];var _0x47af12=crypto[_0xce73('0x14')](_0x5d9ddc,_0x17aeb7);var _0x52e295=_0x47af12[_0xce73('0x1d')](_0x5a20c4,'utf8',_0xce73('0x20'));_0x52e295+=_0x47af12[_0xce73('0x19')](_0xce73('0x20'));return _0x52e295;};exports[_0xce73('0x21')]=function(_0xd2748e,_0x13d670,_0x58706c){var _0x4a1cdb=_0x58706c||stringOptions[_0xce73('0xa')];var _0x482f23=_0x13d670||stringOptions['secret'];var _0x484f7a=crypto[_0xce73('0x1c')](_0x4a1cdb,_0x482f23);var _0x5ccf16=_0x484f7a[_0xce73('0x1d')](_0xd2748e,_0xce73('0x20'),_0xce73('0x22'));_0x5ccf16+=_0x484f7a['final'](_0xce73('0x22'));return _0x5ccf16;};