ea9059c7be13a82e65a05c9fe643044fd8c143c4
[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 _0x77c9=['filter','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','update','write','end','final','binary','close','unlink','decryptFile','createDecipher','data','encryptString','secret','utf8','base64','decryptString','crypto','bluebird','lodash','../../config/logger','app','secrets','session','aes-256-ctr','encrypt','algorithm','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','getCiphers'];(function(_0x26e727,_0x3e65be){var _0x563800=function(_0xbcf999){while(--_0xbcf999){_0x26e727['push'](_0x26e727['shift']());}};_0x563800(++_0x3e65be);}(_0x77c9,0xdb));var _0x977c=function(_0x11d85a,_0x4a908e){_0x11d85a=_0x11d85a-0x0;var _0x5b5638=_0x77c9[_0x11d85a];return _0x5b5638;};'use strict';var crypto=require(_0x977c('0x0'));var fs=require('fs');var BPromise=require(_0x977c('0x1'));var _=require(_0x977c('0x2'));var config=require('../../config/environment');var logger=require(_0x977c('0x3'))(_0x977c('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x977c('0x5')][_0x977c('0x6')],'algorithm':_0x977c('0x7')};const fileOptions={'algorithm':'aes192'};if(config[_0x977c('0x8')]&&config[_0x977c('0x8')][_0x977c('0x9')]){var isSupported=isAlgorithmSupported(config[_0x977c('0x8')][_0x977c('0x9')]);if(isSupported){fileOptions['algorithm']=config['encrypt'][_0x977c('0x9')];}else{var supportedAlgorithms=getSupportedCiphers()['join'](',\x20');logger['error'](config[_0x977c('0x8')][_0x977c('0x9')],_0x977c('0xa'),supportedAlgorithms);}}function isASupportedCipher(_0x313459){return AES_SUPPORTED_RX['test'](_0x313459);}function getSupportedCiphers(){return crypto[_0x977c('0xb')]()[_0x977c('0xc')](isASupportedCipher);}function isAlgorithmSupported(_0x139e5d){return getSupportedCiphers()[_0x977c('0xd')](_0x139e5d)>-0x1;}function combineOptions(_0x3ff20d){var _0x1fa966={};for(var _0x198005 in fileOptions){if(fileOptions[_0x977c('0xe')](_0x198005)){_0x1fa966[_0x198005]=fileOptions[_0x198005];}}if(_0x3ff20d){for(var _0x36a196 in _0x3ff20d){if(_0x3ff20d['hasOwnProperty'](_0x36a196)){_0x1fa966[_0x36a196]=_0x3ff20d[_0x36a196];}}}return _0x1fa966;}exports[_0x977c('0xf')]=function(_0x32f082,_0x1d862a,_0x210f2d,_0x4cfb3b){return new BPromise(function(_0x1290a7,_0x29f673){_0x4cfb3b=combineOptions(_0x4cfb3b);var _0x4e56bb=new Buffer(_0x210f2d);var _0x58de65=fs[_0x977c('0x10')](_0x32f082);var _0x1374bc=fs[_0x977c('0x11')](_0x1d862a);var _0x16147d=crypto['createCipher'](_0x4cfb3b[_0x977c('0x9')],_0x4e56bb);_0x58de65['on']('data',function(_0x22c2bd){var _0x1bb0f8=new Buffer(_0x16147d[_0x977c('0x12')](_0x22c2bd),'binary');_0x1374bc[_0x977c('0x13')](_0x1bb0f8);});_0x58de65['on'](_0x977c('0x14'),function(){try{var _0x3d59c1=new Buffer(_0x16147d[_0x977c('0x15')](_0x977c('0x16')),_0x977c('0x16'));_0x1374bc[_0x977c('0x13')](_0x3d59c1);_0x1374bc['end']();_0x1374bc['on'](_0x977c('0x17'),function(){return _0x1290a7(_0x1d862a);});}catch(_0x3afe74){fs[_0x977c('0x18')](_0x1d862a);return _0x29f673(_0x3afe74);}});});};exports[_0x977c('0x19')]=function(_0x23d74b,_0x58db0f,_0x48c35e,_0x436be9){return new BPromise(function(_0x5b22ac,_0x47936e){_0x436be9=combineOptions(_0x436be9);var _0xc5fde7=new Buffer(_0x48c35e);var _0x346dd9=fs[_0x977c('0x10')](_0x23d74b);var _0xc5d21d=fs[_0x977c('0x11')](_0x58db0f);var _0x45b7b5=crypto[_0x977c('0x1a')](_0x436be9[_0x977c('0x9')],_0xc5fde7);_0x346dd9['on'](_0x977c('0x1b'),function(_0x22ae65){var _0xba7e20=new Buffer(_0x45b7b5[_0x977c('0x12')](_0x22ae65),'binary');_0xc5d21d[_0x977c('0x13')](_0xba7e20);});_0x346dd9['on']('end',function(){try{var _0x5059ef=new Buffer(_0x45b7b5[_0x977c('0x15')](_0x977c('0x16')),'binary');_0xc5d21d[_0x977c('0x13')](_0x5059ef);_0xc5d21d['end']();_0xc5d21d['on'](_0x977c('0x17'),function(){return _0x5b22ac(_0x58db0f);});}catch(_0x18f26c){fs[_0x977c('0x18')](_0x58db0f);return _0x47936e(_0x18f26c);}});});};exports[_0x977c('0x1c')]=function(_0x505a02,_0x449dac,_0x180d3e){var _0x32a88e=_0x180d3e||stringOptions[_0x977c('0x9')];var _0x324957=_0x449dac||stringOptions[_0x977c('0x1d')];var _0x57e362=crypto['createCipher'](_0x32a88e,_0x324957);var _0x186600=_0x57e362[_0x977c('0x12')](_0x505a02,_0x977c('0x1e'),_0x977c('0x1f'));_0x186600+=_0x57e362['final'](_0x977c('0x1f'));return _0x186600;};exports[_0x977c('0x20')]=function(_0x468b1d,_0x24d146,_0x53234f){var _0x914af7=_0x53234f||stringOptions[_0x977c('0x9')];var _0x120030=_0x24d146||stringOptions[_0x977c('0x1d')];var _0x5119b9=crypto['createDecipher'](_0x914af7,_0x120030);var _0xbd8ab4=_0x5119b9[_0x977c('0x12')](_0x468b1d,'base64',_0x977c('0x1e'));_0xbd8ab4+=_0x5119b9[_0x977c('0x15')](_0x977c('0x1e'));return _0xbd8ab4;};