Built motion from commit f9526609.|2.6.29
[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 _0x9e86=['test','getCiphers','filter','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','binary','write','final','end','close','decryptFile','createDecipher','data','update','unlink','encryptString','decryptString','secret','base64','utf8','lodash','../../config/environment','../../config/logger','app','secrets','aes-256-ctr','aes192','encrypt','algorithm','error'];(function(_0x48d0cd,_0x24c3e4){var _0x423c1a=function(_0x524f53){while(--_0x524f53){_0x48d0cd['push'](_0x48d0cd['shift']());}};_0x423c1a(++_0x24c3e4);}(_0x9e86,0x128));var _0x69e8=function(_0x32a10c,_0x59ff50){_0x32a10c=_0x32a10c-0x0;var _0x1910d6=_0x9e86[_0x32a10c];return _0x1910d6;};'use strict';var crypto=require('crypto');var fs=require('fs');var BPromise=require('bluebird');var _=require(_0x69e8('0x0'));var config=require(_0x69e8('0x1'));var logger=require(_0x69e8('0x2'))(_0x69e8('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x69e8('0x4')]['session'],'algorithm':_0x69e8('0x5')};const fileOptions={'algorithm':_0x69e8('0x6')};if(config[_0x69e8('0x7')]&&config[_0x69e8('0x7')][_0x69e8('0x8')]){var isSupported=isAlgorithmSupported(config[_0x69e8('0x7')][_0x69e8('0x8')]);if(isSupported){fileOptions[_0x69e8('0x8')]=config[_0x69e8('0x7')][_0x69e8('0x8')];}else{var supportedAlgorithms=getSupportedCiphers()['join'](',\x20');logger[_0x69e8('0x9')](config[_0x69e8('0x7')]['algorithm'],'is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:',supportedAlgorithms);}}function isASupportedCipher(_0x511dfe){return AES_SUPPORTED_RX[_0x69e8('0xa')](_0x511dfe);}function getSupportedCiphers(){return crypto[_0x69e8('0xb')]()[_0x69e8('0xc')](isASupportedCipher);}function isAlgorithmSupported(_0x3707dc){return getSupportedCiphers()[_0x69e8('0xd')](_0x3707dc)>-0x1;}function combineOptions(_0x2a0747){var _0x13c6d1={};for(var _0x43165e in fileOptions){if(fileOptions[_0x69e8('0xe')](_0x43165e)){_0x13c6d1[_0x43165e]=fileOptions[_0x43165e];}}if(_0x2a0747){for(var _0x55818f in _0x2a0747){if(_0x2a0747['hasOwnProperty'](_0x55818f)){_0x13c6d1[_0x55818f]=_0x2a0747[_0x55818f];}}}return _0x13c6d1;}exports[_0x69e8('0xf')]=function(_0x50078f,_0x392490,_0x133c99,_0x3e47db){return new BPromise(function(_0x3d4c34,_0xdc37e9){_0x3e47db=combineOptions(_0x3e47db);var _0x30411a=new Buffer(_0x133c99);var _0x50e030=fs[_0x69e8('0x10')](_0x50078f);var _0x18fa16=fs[_0x69e8('0x11')](_0x392490);var _0x4564c8=crypto[_0x69e8('0x12')](_0x3e47db[_0x69e8('0x8')],_0x30411a);_0x50e030['on']('data',function(_0x395ae8){var _0x11132d=new Buffer(_0x4564c8['update'](_0x395ae8),_0x69e8('0x13'));_0x18fa16[_0x69e8('0x14')](_0x11132d);});_0x50e030['on']('end',function(){try{var _0x126c73=new Buffer(_0x4564c8[_0x69e8('0x15')](_0x69e8('0x13')),'binary');_0x18fa16[_0x69e8('0x14')](_0x126c73);_0x18fa16[_0x69e8('0x16')]();_0x18fa16['on'](_0x69e8('0x17'),function(){return _0x3d4c34(_0x392490);});}catch(_0x19c883){fs['unlink'](_0x392490);return _0xdc37e9(_0x19c883);}});});};exports[_0x69e8('0x18')]=function(_0x55927f,_0x36c5fa,_0x38fad4,_0x355076){return new BPromise(function(_0x2668d9,_0x363598){_0x355076=combineOptions(_0x355076);var _0x40eb19=new Buffer(_0x38fad4);var _0x1edf14=fs[_0x69e8('0x10')](_0x55927f);var _0x554126=fs[_0x69e8('0x11')](_0x36c5fa);var _0xb64398=crypto[_0x69e8('0x19')](_0x355076[_0x69e8('0x8')],_0x40eb19);_0x1edf14['on'](_0x69e8('0x1a'),function(_0x1885f4){var _0x3f781f=new Buffer(_0xb64398[_0x69e8('0x1b')](_0x1885f4),_0x69e8('0x13'));_0x554126[_0x69e8('0x14')](_0x3f781f);});_0x1edf14['on']('end',function(){try{var _0x194d6e=new Buffer(_0xb64398['final'](_0x69e8('0x13')),_0x69e8('0x13'));_0x554126[_0x69e8('0x14')](_0x194d6e);_0x554126[_0x69e8('0x16')]();_0x554126['on'](_0x69e8('0x17'),function(){return _0x2668d9(_0x36c5fa);});}catch(_0x4087cc){fs[_0x69e8('0x1c')](_0x36c5fa);return _0x363598(_0x4087cc);}});});};exports[_0x69e8('0x1d')]=function(_0x212104,_0x3c7b3d,_0x2c99db){var _0xd3f484=_0x2c99db||stringOptions[_0x69e8('0x8')];var _0x192a13=_0x3c7b3d||stringOptions['secret'];var _0x42ce70=crypto[_0x69e8('0x12')](_0xd3f484,_0x192a13);var _0x12a6a2=_0x42ce70[_0x69e8('0x1b')](_0x212104,'utf8','base64');_0x12a6a2+=_0x42ce70['final']('base64');return _0x12a6a2;};exports[_0x69e8('0x1e')]=function(_0x57fe87,_0x1562e3,_0x292d7d){var _0x15d54b=_0x292d7d||stringOptions['algorithm'];var _0x1a1faa=_0x1562e3||stringOptions[_0x69e8('0x1f')];var _0x477fe7=crypto[_0x69e8('0x19')](_0x15d54b,_0x1a1faa);var _0x1858a7=_0x477fe7[_0x69e8('0x1b')](_0x57fe87,_0x69e8('0x20'),_0x69e8('0x21'));_0x1858a7+=_0x477fe7['final']('utf8');return _0x1858a7;};