Built motion from commit 64a6f4bc.|2.6.26
[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 _0x4e51=['final','end','unlink','decryptFile','data','update','close','encryptString','secret','utf8','base64','decryptString','crypto','bluebird','lodash','../../config/logger','app','secrets','session','aes-256-ctr','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','filter','indexOf','hasOwnProperty','createReadStream','createCipher','binary','write'];(function(_0x2c901d,_0x656a41){var _0x336109=function(_0x545b4b){while(--_0x545b4b){_0x2c901d['push'](_0x2c901d['shift']());}};_0x336109(++_0x656a41);}(_0x4e51,0x1a4));var _0x14e5=function(_0x4cf8d4,_0x5aee73){_0x4cf8d4=_0x4cf8d4-0x0;var _0x2c1beb=_0x4e51[_0x4cf8d4];return _0x2c1beb;};'use strict';var crypto=require(_0x14e5('0x0'));var fs=require('fs');var BPromise=require(_0x14e5('0x1'));var _=require(_0x14e5('0x2'));var config=require('../../config/environment');var logger=require(_0x14e5('0x3'))(_0x14e5('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x14e5('0x5')][_0x14e5('0x6')],'algorithm':_0x14e5('0x7')};const fileOptions={'algorithm':'aes192'};if(config['encrypt']&&config[_0x14e5('0x8')][_0x14e5('0x9')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0x14e5('0x9')]);if(isSupported){fileOptions['algorithm']=config[_0x14e5('0x8')][_0x14e5('0x9')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x14e5('0xa')](',\x20');logger[_0x14e5('0xb')](config['encrypt']['algorithm'],_0x14e5('0xc'),supportedAlgorithms);}}function isASupportedCipher(_0x2cd881){return AES_SUPPORTED_RX[_0x14e5('0xd')](_0x2cd881);}function getSupportedCiphers(){return crypto[_0x14e5('0xe')]()[_0x14e5('0xf')](isASupportedCipher);}function isAlgorithmSupported(_0x4db3e4){return getSupportedCiphers()[_0x14e5('0x10')](_0x4db3e4)>-0x1;}function combineOptions(_0x355113){var _0x496fcb={};for(var _0x2418d5 in fileOptions){if(fileOptions[_0x14e5('0x11')](_0x2418d5)){_0x496fcb[_0x2418d5]=fileOptions[_0x2418d5];}}if(_0x355113){for(var _0x160bbf in _0x355113){if(_0x355113['hasOwnProperty'](_0x160bbf)){_0x496fcb[_0x160bbf]=_0x355113[_0x160bbf];}}}return _0x496fcb;}exports['encryptFile']=function(_0x2f0402,_0x243640,_0x22db8d,_0x2e65b6){return new BPromise(function(_0x519085,_0xf84dcf){_0x2e65b6=combineOptions(_0x2e65b6);var _0x441fc2=new Buffer(_0x22db8d);var _0x43732a=fs[_0x14e5('0x12')](_0x2f0402);var _0x1187f2=fs['createWriteStream'](_0x243640);var _0x4e164f=crypto[_0x14e5('0x13')](_0x2e65b6[_0x14e5('0x9')],_0x441fc2);_0x43732a['on']('data',function(_0x14dfce){var _0x1b2020=new Buffer(_0x4e164f['update'](_0x14dfce),_0x14e5('0x14'));_0x1187f2[_0x14e5('0x15')](_0x1b2020);});_0x43732a['on']('end',function(){try{var _0xc958ec=new Buffer(_0x4e164f[_0x14e5('0x16')](_0x14e5('0x14')),'binary');_0x1187f2[_0x14e5('0x15')](_0xc958ec);_0x1187f2[_0x14e5('0x17')]();_0x1187f2['on']('close',function(){return _0x519085(_0x243640);});}catch(_0x154d19){fs[_0x14e5('0x18')](_0x243640);return _0xf84dcf(_0x154d19);}});});};exports[_0x14e5('0x19')]=function(_0x115068,_0x5af6d8,_0x33c5f2,_0x3df082){return new BPromise(function(_0x248437,_0x34ca3d){_0x3df082=combineOptions(_0x3df082);var _0x5d31d0=new Buffer(_0x33c5f2);var _0x585214=fs['createReadStream'](_0x115068);var _0x151d3b=fs['createWriteStream'](_0x5af6d8);var _0x26c297=crypto['createDecipher'](_0x3df082[_0x14e5('0x9')],_0x5d31d0);_0x585214['on'](_0x14e5('0x1a'),function(_0x594f15){var _0x583733=new Buffer(_0x26c297[_0x14e5('0x1b')](_0x594f15),'binary');_0x151d3b[_0x14e5('0x15')](_0x583733);});_0x585214['on'](_0x14e5('0x17'),function(){try{var _0xd283ef=new Buffer(_0x26c297[_0x14e5('0x16')](_0x14e5('0x14')),_0x14e5('0x14'));_0x151d3b[_0x14e5('0x15')](_0xd283ef);_0x151d3b[_0x14e5('0x17')]();_0x151d3b['on'](_0x14e5('0x1c'),function(){return _0x248437(_0x5af6d8);});}catch(_0x19dd92){fs[_0x14e5('0x18')](_0x5af6d8);return _0x34ca3d(_0x19dd92);}});});};exports[_0x14e5('0x1d')]=function(_0x151c12,_0x1ee9f5,_0x1feca8){var _0x514af6=_0x1feca8||stringOptions['algorithm'];var _0x49f7ba=_0x1ee9f5||stringOptions[_0x14e5('0x1e')];var _0x20472a=crypto[_0x14e5('0x13')](_0x514af6,_0x49f7ba);var _0x488bfa=_0x20472a[_0x14e5('0x1b')](_0x151c12,_0x14e5('0x1f'),_0x14e5('0x20'));_0x488bfa+=_0x20472a['final']('base64');return _0x488bfa;};exports[_0x14e5('0x21')]=function(_0x1d3a79,_0x36dd72,_0x3f39f9){var _0x14edd8=_0x3f39f9||stringOptions['algorithm'];var _0x31deb2=_0x36dd72||stringOptions[_0x14e5('0x1e')];var _0x36e51d=crypto['createDecipher'](_0x14edd8,_0x31deb2);var _0x4c4ae8=_0x36e51d[_0x14e5('0x1b')](_0x1d3a79,'base64','utf8');_0x4c4ae8+=_0x36e51d[_0x14e5('0x16')]('utf8');return _0x4c4ae8;};