a5a6a4cd18ec980c940569ce7c0398a6ffce07f4
[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 _0xc325=['aes192','encrypt','algorithm','join','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','filter','indexOf','hasOwnProperty','encryptFile','createReadStream','createCipher','data','update','binary','write','end','unlink','decryptFile','createWriteStream','createDecipher','final','close','encryptString','secret','utf8','base64','decryptString','crypto','lodash','../../config/environment','../../config/logger','app','secrets'];(function(_0x22ee0f,_0x22e6d2){var _0x204696=function(_0x4cab38){while(--_0x4cab38){_0x22ee0f['push'](_0x22ee0f['shift']());}};_0x204696(++_0x22e6d2);}(_0xc325,0x112));var _0x5c32=function(_0x3bf83f,_0x181cac){_0x3bf83f=_0x3bf83f-0x0;var _0x22c4a9=_0xc325[_0x3bf83f];return _0x22c4a9;};'use strict';var crypto=require(_0x5c32('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require(_0x5c32('0x1'));var config=require(_0x5c32('0x2'));var logger=require(_0x5c32('0x3'))(_0x5c32('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x5c32('0x5')]['session'],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0x5c32('0x6')};if(config[_0x5c32('0x7')]&&config[_0x5c32('0x7')][_0x5c32('0x8')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0x5c32('0x8')]);if(isSupported){fileOptions[_0x5c32('0x8')]=config[_0x5c32('0x7')]['algorithm'];}else{var supportedAlgorithms=getSupportedCiphers()[_0x5c32('0x9')](',\x20');logger['error'](config[_0x5c32('0x7')][_0x5c32('0x8')],_0x5c32('0xa'),supportedAlgorithms);}}function isASupportedCipher(_0x2761c3){return AES_SUPPORTED_RX[_0x5c32('0xb')](_0x2761c3);}function getSupportedCiphers(){return crypto[_0x5c32('0xc')]()[_0x5c32('0xd')](isASupportedCipher);}function isAlgorithmSupported(_0xf20692){return getSupportedCiphers()[_0x5c32('0xe')](_0xf20692)>-0x1;}function combineOptions(_0x41be88){var _0x15d004={};for(var _0x2bba0d in fileOptions){if(fileOptions[_0x5c32('0xf')](_0x2bba0d)){_0x15d004[_0x2bba0d]=fileOptions[_0x2bba0d];}}if(_0x41be88){for(var _0x35e7b7 in _0x41be88){if(_0x41be88[_0x5c32('0xf')](_0x35e7b7)){_0x15d004[_0x35e7b7]=_0x41be88[_0x35e7b7];}}}return _0x15d004;}exports[_0x5c32('0x10')]=function(_0xf2af49,_0x59de10,_0x1dec58,_0x52110d){return new BPromise(function(_0x423b12,_0x2bbf4a){_0x52110d=combineOptions(_0x52110d);var _0x161924=new Buffer(_0x1dec58);var _0x32c9dd=fs[_0x5c32('0x11')](_0xf2af49);var _0x4043d9=fs['createWriteStream'](_0x59de10);var _0xc9562=crypto[_0x5c32('0x12')](_0x52110d[_0x5c32('0x8')],_0x161924);_0x32c9dd['on'](_0x5c32('0x13'),function(_0x5f2dcf){var _0x5c4606=new Buffer(_0xc9562[_0x5c32('0x14')](_0x5f2dcf),_0x5c32('0x15'));_0x4043d9[_0x5c32('0x16')](_0x5c4606);});_0x32c9dd['on'](_0x5c32('0x17'),function(){try{var _0x5db851=new Buffer(_0xc9562['final']('binary'),'binary');_0x4043d9['write'](_0x5db851);_0x4043d9[_0x5c32('0x17')]();_0x4043d9['on']('close',function(){return _0x423b12(_0x59de10);});}catch(_0x3477f7){fs[_0x5c32('0x18')](_0x59de10);return _0x2bbf4a(_0x3477f7);}});});};exports[_0x5c32('0x19')]=function(_0x37488e,_0x1f2676,_0x4da24e,_0x566c28){return new BPromise(function(_0x6d7124,_0x29dce0){_0x566c28=combineOptions(_0x566c28);var _0x5a0b3f=new Buffer(_0x4da24e);var _0x4722c8=fs[_0x5c32('0x11')](_0x37488e);var _0x3342e0=fs[_0x5c32('0x1a')](_0x1f2676);var _0xc899e1=crypto[_0x5c32('0x1b')](_0x566c28['algorithm'],_0x5a0b3f);_0x4722c8['on']('data',function(_0x4577f4){var _0x4e2f3a=new Buffer(_0xc899e1['update'](_0x4577f4),_0x5c32('0x15'));_0x3342e0['write'](_0x4e2f3a);});_0x4722c8['on']('end',function(){try{var _0x557365=new Buffer(_0xc899e1[_0x5c32('0x1c')](_0x5c32('0x15')),_0x5c32('0x15'));_0x3342e0[_0x5c32('0x16')](_0x557365);_0x3342e0['end']();_0x3342e0['on'](_0x5c32('0x1d'),function(){return _0x6d7124(_0x1f2676);});}catch(_0xa176bb){fs[_0x5c32('0x18')](_0x1f2676);return _0x29dce0(_0xa176bb);}});});};exports[_0x5c32('0x1e')]=function(_0x23f659,_0x446068,_0x11bb22){var _0xa9188d=_0x11bb22||stringOptions[_0x5c32('0x8')];var _0x34f9ff=_0x446068||stringOptions[_0x5c32('0x1f')];var _0xa9b86c=crypto[_0x5c32('0x12')](_0xa9188d,_0x34f9ff);var _0x3b6c81=_0xa9b86c[_0x5c32('0x14')](_0x23f659,_0x5c32('0x20'),_0x5c32('0x21'));_0x3b6c81+=_0xa9b86c[_0x5c32('0x1c')](_0x5c32('0x21'));return _0x3b6c81;};exports[_0x5c32('0x22')]=function(_0x307a1d,_0x654bd2,_0x28a59c){var _0x11f41f=_0x28a59c||stringOptions[_0x5c32('0x8')];var _0x23496d=_0x654bd2||stringOptions[_0x5c32('0x1f')];var _0x3ab11d=crypto['createDecipher'](_0x11f41f,_0x23496d);var _0x3c04d7=_0x3ab11d[_0x5c32('0x14')](_0x307a1d,'base64',_0x5c32('0x20'));_0x3c04d7+=_0x3ab11d[_0x5c32('0x1c')]('utf8');return _0x3c04d7;};