Built motion from commit b58257bb.|2.5.37
[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 _0x2752=['encryptFile','createReadStream','createWriteStream','createCipher','update','write','final','binary','unlink','end','close','encryptString','secret','utf8','base64','decryptString','createDecipher','crypto','../../config/environment','../../config/logger','app','secrets','aes192','encrypt','algorithm','join','error','getCiphers','filter','indexOf','hasOwnProperty'];(function(_0x2c34cf,_0x43aacc){var _0x44969e=function(_0x59cb35){while(--_0x59cb35){_0x2c34cf['push'](_0x2c34cf['shift']());}};_0x44969e(++_0x43aacc);}(_0x2752,0x6e));var _0x2275=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0x2752[_0x3dd15e];return _0x231fd0;};'use strict';var crypto=require(_0x2275('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require('lodash');var config=require(_0x2275('0x1'));var logger=require(_0x2275('0x2'))(_0x2275('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x2275('0x4')]['session'],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0x2275('0x5')};if(config['encrypt']&&config['encrypt']['algorithm']){var isSupported=isAlgorithmSupported(config[_0x2275('0x6')][_0x2275('0x7')]);if(isSupported){fileOptions[_0x2275('0x7')]=config[_0x2275('0x6')]['algorithm'];}else{var supportedAlgorithms=getSupportedCiphers()[_0x2275('0x8')](',\x20');logger[_0x2275('0x9')](config[_0x2275('0x6')]['algorithm'],'is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:',supportedAlgorithms);}}function isASupportedCipher(_0x12974f){return AES_SUPPORTED_RX['test'](_0x12974f);}function getSupportedCiphers(){return crypto[_0x2275('0xa')]()[_0x2275('0xb')](isASupportedCipher);}function isAlgorithmSupported(_0x5e41b0){return getSupportedCiphers()[_0x2275('0xc')](_0x5e41b0)>-0x1;}function combineOptions(_0x2d3f86){var _0xed7a20={};for(var _0x455664 in fileOptions){if(fileOptions[_0x2275('0xd')](_0x455664)){_0xed7a20[_0x455664]=fileOptions[_0x455664];}}if(_0x2d3f86){for(var _0x543c77 in _0x2d3f86){if(_0x2d3f86[_0x2275('0xd')](_0x543c77)){_0xed7a20[_0x543c77]=_0x2d3f86[_0x543c77];}}}return _0xed7a20;}exports[_0x2275('0xe')]=function(_0xe73acc,_0x4f3547,_0x46a8cf,_0x2cf4b9){return new BPromise(function(_0x4201fd,_0x4fcd08){_0x2cf4b9=combineOptions(_0x2cf4b9);var _0x5cf7e5=new Buffer(_0x46a8cf);var _0x1b42ab=fs[_0x2275('0xf')](_0xe73acc);var _0x4344af=fs[_0x2275('0x10')](_0x4f3547);var _0x3ae5f4=crypto[_0x2275('0x11')](_0x2cf4b9[_0x2275('0x7')],_0x5cf7e5);_0x1b42ab['on']('data',function(_0x650d9c){var _0x52734a=new Buffer(_0x3ae5f4[_0x2275('0x12')](_0x650d9c),'binary');_0x4344af[_0x2275('0x13')](_0x52734a);});_0x1b42ab['on']('end',function(){try{var _0x1e891e=new Buffer(_0x3ae5f4[_0x2275('0x14')](_0x2275('0x15')),_0x2275('0x15'));_0x4344af[_0x2275('0x13')](_0x1e891e);_0x4344af['end']();_0x4344af['on']('close',function(){return _0x4201fd(_0x4f3547);});}catch(_0x27bdfc){fs[_0x2275('0x16')](_0x4f3547);return _0x4fcd08(_0x27bdfc);}});});};exports['decryptFile']=function(_0x14fb23,_0x1c1d33,_0x4121ef,_0x11fb6b){return new BPromise(function(_0x4be9cd,_0x40d640){_0x11fb6b=combineOptions(_0x11fb6b);var _0x207c6c=new Buffer(_0x4121ef);var _0xe4ef13=fs['createReadStream'](_0x14fb23);var _0x3adac1=fs['createWriteStream'](_0x1c1d33);var _0x5694dd=crypto['createDecipher'](_0x11fb6b[_0x2275('0x7')],_0x207c6c);_0xe4ef13['on']('data',function(_0x297a77){var _0x153c10=new Buffer(_0x5694dd[_0x2275('0x12')](_0x297a77),_0x2275('0x15'));_0x3adac1[_0x2275('0x13')](_0x153c10);});_0xe4ef13['on']('end',function(){try{var _0x438596=new Buffer(_0x5694dd[_0x2275('0x14')](_0x2275('0x15')),_0x2275('0x15'));_0x3adac1['write'](_0x438596);_0x3adac1[_0x2275('0x17')]();_0x3adac1['on'](_0x2275('0x18'),function(){return _0x4be9cd(_0x1c1d33);});}catch(_0x11e779){fs['unlink'](_0x1c1d33);return _0x40d640(_0x11e779);}});});};exports[_0x2275('0x19')]=function(_0x3a24c5,_0x3d9014,_0x159837){var _0x590a6e=_0x159837||stringOptions['algorithm'];var _0x37c78c=_0x3d9014||stringOptions[_0x2275('0x1a')];var _0x306a19=crypto[_0x2275('0x11')](_0x590a6e,_0x37c78c);var _0x56ebb7=_0x306a19[_0x2275('0x12')](_0x3a24c5,_0x2275('0x1b'),_0x2275('0x1c'));_0x56ebb7+=_0x306a19[_0x2275('0x14')](_0x2275('0x1c'));return _0x56ebb7;};exports[_0x2275('0x1d')]=function(_0x32101b,_0x4d0927,_0x42b533){var _0x246f2c=_0x42b533||stringOptions[_0x2275('0x7')];var _0xb31ad3=_0x4d0927||stringOptions['secret'];var _0xa097b6=crypto[_0x2275('0x1e')](_0x246f2c,_0xb31ad3);var _0x816593=_0xa097b6[_0x2275('0x12')](_0x32101b,_0x2275('0x1c'),_0x2275('0x1b'));_0x816593+=_0xa097b6[_0x2275('0x14')]('utf8');return _0x816593;};