Built motion from commit da24aabd.|2.6.20
[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 _0x87d6=['encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','data','binary','write','end','close','unlink','decryptFile','createDecipher','update','utf8','base64','final','decryptString','secret','lodash','../../config/environment','../../config/logger','app','secrets','session','aes-256-ctr','aes192'];(function(_0xb9a43a,_0x14b436){var _0xedabd8=function(_0x4142fe){while(--_0x4142fe){_0xb9a43a['push'](_0xb9a43a['shift']());}};_0xedabd8(++_0x14b436);}(_0x87d6,0xbe));var _0x687d=function(_0x3740fe,_0x6c543c){_0x3740fe=_0x3740fe-0x0;var _0x55492b=_0x87d6[_0x3740fe];return _0x55492b;};'use strict';var crypto=require('crypto');var fs=require('fs');var BPromise=require('bluebird');var _=require(_0x687d('0x0'));var config=require(_0x687d('0x1'));var logger=require(_0x687d('0x2'))(_0x687d('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x687d('0x4')][_0x687d('0x5')],'algorithm':_0x687d('0x6')};const fileOptions={'algorithm':_0x687d('0x7')};if(config[_0x687d('0x8')]&&config[_0x687d('0x8')]['algorithm']){var isSupported=isAlgorithmSupported(config[_0x687d('0x8')]['algorithm']);if(isSupported){fileOptions[_0x687d('0x9')]=config[_0x687d('0x8')][_0x687d('0x9')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x687d('0xa')](',\x20');logger[_0x687d('0xb')](config[_0x687d('0x8')][_0x687d('0x9')],_0x687d('0xc'),supportedAlgorithms);}}function isASupportedCipher(_0xc3ddbe){return AES_SUPPORTED_RX['test'](_0xc3ddbe);}function getSupportedCiphers(){return crypto['getCiphers']()['filter'](isASupportedCipher);}function isAlgorithmSupported(_0x3c4078){return getSupportedCiphers()[_0x687d('0xd')](_0x3c4078)>-0x1;}function combineOptions(_0xb29b25){var _0x5c3823={};for(var _0x5338ea in fileOptions){if(fileOptions['hasOwnProperty'](_0x5338ea)){_0x5c3823[_0x5338ea]=fileOptions[_0x5338ea];}}if(_0xb29b25){for(var _0x399cbc in _0xb29b25){if(_0xb29b25[_0x687d('0xe')](_0x399cbc)){_0x5c3823[_0x399cbc]=_0xb29b25[_0x399cbc];}}}return _0x5c3823;}exports[_0x687d('0xf')]=function(_0x4d0f06,_0x18bfdb,_0x3645e0,_0x4903a8){return new BPromise(function(_0x10a2a5,_0x3a55a2){_0x4903a8=combineOptions(_0x4903a8);var _0x5e933a=new Buffer(_0x3645e0);var _0x48bd8a=fs[_0x687d('0x10')](_0x4d0f06);var _0x1c2bdc=fs[_0x687d('0x11')](_0x18bfdb);var _0xbf03b7=crypto[_0x687d('0x12')](_0x4903a8[_0x687d('0x9')],_0x5e933a);_0x48bd8a['on'](_0x687d('0x13'),function(_0x4cc976){var _0xe6fa35=new Buffer(_0xbf03b7['update'](_0x4cc976),_0x687d('0x14'));_0x1c2bdc[_0x687d('0x15')](_0xe6fa35);});_0x48bd8a['on'](_0x687d('0x16'),function(){try{var _0xf7d52f=new Buffer(_0xbf03b7['final'](_0x687d('0x14')),_0x687d('0x14'));_0x1c2bdc[_0x687d('0x15')](_0xf7d52f);_0x1c2bdc[_0x687d('0x16')]();_0x1c2bdc['on'](_0x687d('0x17'),function(){return _0x10a2a5(_0x18bfdb);});}catch(_0xaa59d5){fs[_0x687d('0x18')](_0x18bfdb);return _0x3a55a2(_0xaa59d5);}});});};exports[_0x687d('0x19')]=function(_0x452a30,_0x9acc13,_0x17d578,_0x2cd74b){return new BPromise(function(_0x584749,_0x3c6900){_0x2cd74b=combineOptions(_0x2cd74b);var _0x5ab64e=new Buffer(_0x17d578);var _0x59d30d=fs[_0x687d('0x10')](_0x452a30);var _0x3d5582=fs[_0x687d('0x11')](_0x9acc13);var _0x4f6a7e=crypto[_0x687d('0x1a')](_0x2cd74b[_0x687d('0x9')],_0x5ab64e);_0x59d30d['on']('data',function(_0x2bdbc9){var _0x26faaa=new Buffer(_0x4f6a7e[_0x687d('0x1b')](_0x2bdbc9),_0x687d('0x14'));_0x3d5582['write'](_0x26faaa);});_0x59d30d['on'](_0x687d('0x16'),function(){try{var _0x2cdce3=new Buffer(_0x4f6a7e['final'](_0x687d('0x14')),_0x687d('0x14'));_0x3d5582[_0x687d('0x15')](_0x2cdce3);_0x3d5582['end']();_0x3d5582['on'](_0x687d('0x17'),function(){return _0x584749(_0x9acc13);});}catch(_0xd4605d){fs['unlink'](_0x9acc13);return _0x3c6900(_0xd4605d);}});});};exports['encryptString']=function(_0x51f608,_0x28c620,_0x539a30){var _0x21e1d5=_0x539a30||stringOptions[_0x687d('0x9')];var _0x1591f1=_0x28c620||stringOptions['secret'];var _0x1118fc=crypto[_0x687d('0x12')](_0x21e1d5,_0x1591f1);var _0x5da1cf=_0x1118fc[_0x687d('0x1b')](_0x51f608,_0x687d('0x1c'),_0x687d('0x1d'));_0x5da1cf+=_0x1118fc[_0x687d('0x1e')](_0x687d('0x1d'));return _0x5da1cf;};exports[_0x687d('0x1f')]=function(_0x3602c1,_0x5c8845,_0x289085){var _0x199f84=_0x289085||stringOptions[_0x687d('0x9')];var _0x740452=_0x5c8845||stringOptions[_0x687d('0x20')];var _0x2656a5=crypto['createDecipher'](_0x199f84,_0x740452);var _0x3c5741=_0x2656a5['update'](_0x3602c1,_0x687d('0x1d'),_0x687d('0x1c'));_0x3c5741+=_0x2656a5[_0x687d('0x1e')](_0x687d('0x1c'));return _0x3c5741;};