f24be170ea5f194790d2ee83b098a14638b0a242
[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 _0x8dec=['filter','hasOwnProperty','encryptFile','createReadStream','createCipher','data','update','binary','write','final','end','close','unlink','decryptFile','createWriteStream','createDecipher','secret','utf8','base64','decryptString','bluebird','lodash','../../config/logger','secrets','session','aes-256-ctr','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers'];(function(_0x46914a,_0x16fe5f){var _0x520c41=function(_0x3c6142){while(--_0x3c6142){_0x46914a['push'](_0x46914a['shift']());}};_0x520c41(++_0x16fe5f);}(_0x8dec,0x9c));var _0xc8de=function(_0x50d94a,_0x4bb055){_0x50d94a=_0x50d94a-0x0;var _0x4b88ec=_0x8dec[_0x50d94a];return _0x4b88ec;};'use strict';var crypto=require('crypto');var fs=require('fs');var BPromise=require(_0xc8de('0x0'));var _=require(_0xc8de('0x1'));var config=require('../../config/environment');var logger=require(_0xc8de('0x2'))('app');const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xc8de('0x3')][_0xc8de('0x4')],'algorithm':_0xc8de('0x5')};const fileOptions={'algorithm':_0xc8de('0x6')};if(config[_0xc8de('0x7')]&&config[_0xc8de('0x7')]['algorithm']){var isSupported=isAlgorithmSupported(config[_0xc8de('0x7')][_0xc8de('0x8')]);if(isSupported){fileOptions['algorithm']=config[_0xc8de('0x7')][_0xc8de('0x8')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xc8de('0x9')](',\x20');logger[_0xc8de('0xa')](config[_0xc8de('0x7')][_0xc8de('0x8')],_0xc8de('0xb'),supportedAlgorithms);}}function isASupportedCipher(_0x5de509){return AES_SUPPORTED_RX[_0xc8de('0xc')](_0x5de509);}function getSupportedCiphers(){return crypto[_0xc8de('0xd')]()[_0xc8de('0xe')](isASupportedCipher);}function isAlgorithmSupported(_0x219dae){return getSupportedCiphers()['indexOf'](_0x219dae)>-0x1;}function combineOptions(_0x118b06){var _0x5416f5={};for(var _0x1af03e in fileOptions){if(fileOptions[_0xc8de('0xf')](_0x1af03e)){_0x5416f5[_0x1af03e]=fileOptions[_0x1af03e];}}if(_0x118b06){for(var _0x4b4df6 in _0x118b06){if(_0x118b06[_0xc8de('0xf')](_0x4b4df6)){_0x5416f5[_0x4b4df6]=_0x118b06[_0x4b4df6];}}}return _0x5416f5;}exports[_0xc8de('0x10')]=function(_0x4977a9,_0x465a2d,_0x91f5d8,_0x318e6e){return new BPromise(function(_0xf05c68,_0x2833b2){_0x318e6e=combineOptions(_0x318e6e);var _0x30a3b7=new Buffer(_0x91f5d8);var _0x155b17=fs[_0xc8de('0x11')](_0x4977a9);var _0x4f4f21=fs['createWriteStream'](_0x465a2d);var _0x1e8771=crypto[_0xc8de('0x12')](_0x318e6e[_0xc8de('0x8')],_0x30a3b7);_0x155b17['on'](_0xc8de('0x13'),function(_0xcb8563){var _0x5dccb8=new Buffer(_0x1e8771[_0xc8de('0x14')](_0xcb8563),_0xc8de('0x15'));_0x4f4f21[_0xc8de('0x16')](_0x5dccb8);});_0x155b17['on']('end',function(){try{var _0x31f4ec=new Buffer(_0x1e8771[_0xc8de('0x17')](_0xc8de('0x15')),_0xc8de('0x15'));_0x4f4f21[_0xc8de('0x16')](_0x31f4ec);_0x4f4f21[_0xc8de('0x18')]();_0x4f4f21['on'](_0xc8de('0x19'),function(){return _0xf05c68(_0x465a2d);});}catch(_0x1a4a20){fs[_0xc8de('0x1a')](_0x465a2d);return _0x2833b2(_0x1a4a20);}});});};exports[_0xc8de('0x1b')]=function(_0x1755f,_0x279134,_0x4e2fde,_0x29ed2e){return new BPromise(function(_0x1f0a8a,_0x363378){_0x29ed2e=combineOptions(_0x29ed2e);var _0x26fd5a=new Buffer(_0x4e2fde);var _0x42b293=fs[_0xc8de('0x11')](_0x1755f);var _0x1e633b=fs[_0xc8de('0x1c')](_0x279134);var _0x31726b=crypto[_0xc8de('0x1d')](_0x29ed2e[_0xc8de('0x8')],_0x26fd5a);_0x42b293['on'](_0xc8de('0x13'),function(_0x2114e6){var _0x25d2af=new Buffer(_0x31726b[_0xc8de('0x14')](_0x2114e6),'binary');_0x1e633b[_0xc8de('0x16')](_0x25d2af);});_0x42b293['on']('end',function(){try{var _0x2c9519=new Buffer(_0x31726b['final'](_0xc8de('0x15')),'binary');_0x1e633b['write'](_0x2c9519);_0x1e633b[_0xc8de('0x18')]();_0x1e633b['on'](_0xc8de('0x19'),function(){return _0x1f0a8a(_0x279134);});}catch(_0x5525a1){fs[_0xc8de('0x1a')](_0x279134);return _0x363378(_0x5525a1);}});});};exports['encryptString']=function(_0x37916e,_0x5d5127,_0x508c74){var _0x57b58e=_0x508c74||stringOptions['algorithm'];var _0x313032=_0x5d5127||stringOptions[_0xc8de('0x1e')];var _0x215c90=crypto[_0xc8de('0x12')](_0x57b58e,_0x313032);var _0x1dfe80=_0x215c90['update'](_0x37916e,_0xc8de('0x1f'),_0xc8de('0x20'));_0x1dfe80+=_0x215c90['final']('base64');return _0x1dfe80;};exports[_0xc8de('0x21')]=function(_0x4d53f6,_0x4e5202,_0x20709c){var _0x522761=_0x20709c||stringOptions[_0xc8de('0x8')];var _0x30233c=_0x4e5202||stringOptions['secret'];var _0x1888ad=crypto[_0xc8de('0x1d')](_0x522761,_0x30233c);var _0x421c85=_0x1888ad[_0xc8de('0x14')](_0x4d53f6,_0xc8de('0x20'),_0xc8de('0x1f'));_0x421c85+=_0x1888ad[_0xc8de('0x17')](_0xc8de('0x1f'));return _0x421c85;};