06a0d439bbfcb224e0fcbd6a5e890df30e10ceb5
[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 _0xe2ad=['base64','crypto','bluebird','lodash','../../config/environment','app','session','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','filter','indexOf','hasOwnProperty','createWriteStream','createCipher','binary','write','end','final','close','unlink','decryptFile','createReadStream','createDecipher','data','secret','update','utf8'];(function(_0x42a562,_0x1e2b54){var _0x340e5a=function(_0x19990e){while(--_0x19990e){_0x42a562['push'](_0x42a562['shift']());}};_0x340e5a(++_0x1e2b54);}(_0xe2ad,0x12a));var _0xde2a=function(_0x13ba21,_0x190d6e){_0x13ba21=_0x13ba21-0x0;var _0x4307f6=_0xe2ad[_0x13ba21];return _0x4307f6;};'use strict';var crypto=require(_0xde2a('0x0'));var fs=require('fs');var BPromise=require(_0xde2a('0x1'));var _=require(_0xde2a('0x2'));var config=require(_0xde2a('0x3'));var logger=require('../../config/logger')(_0xde2a('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config['secrets'][_0xde2a('0x5')],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0xde2a('0x6')};if(config[_0xde2a('0x7')]&&config[_0xde2a('0x7')][_0xde2a('0x8')]){var isSupported=isAlgorithmSupported(config[_0xde2a('0x7')][_0xde2a('0x8')]);if(isSupported){fileOptions[_0xde2a('0x8')]=config['encrypt'][_0xde2a('0x8')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xde2a('0x9')](',\x20');logger[_0xde2a('0xa')](config[_0xde2a('0x7')]['algorithm'],_0xde2a('0xb'),supportedAlgorithms);}}function isASupportedCipher(_0x1dff82){return AES_SUPPORTED_RX[_0xde2a('0xc')](_0x1dff82);}function getSupportedCiphers(){return crypto[_0xde2a('0xd')]()[_0xde2a('0xe')](isASupportedCipher);}function isAlgorithmSupported(_0x30cc8e){return getSupportedCiphers()[_0xde2a('0xf')](_0x30cc8e)>-0x1;}function combineOptions(_0x29cf57){var _0x302ad6={};for(var _0x5de86a in fileOptions){if(fileOptions['hasOwnProperty'](_0x5de86a)){_0x302ad6[_0x5de86a]=fileOptions[_0x5de86a];}}if(_0x29cf57){for(var _0x2f8e14 in _0x29cf57){if(_0x29cf57[_0xde2a('0x10')](_0x2f8e14)){_0x302ad6[_0x2f8e14]=_0x29cf57[_0x2f8e14];}}}return _0x302ad6;}exports['encryptFile']=function(_0x14a4ae,_0x44dddb,_0x219628,_0x2c66d3){return new BPromise(function(_0x281e86,_0x4632aa){_0x2c66d3=combineOptions(_0x2c66d3);var _0x57465c=new Buffer(_0x219628);var _0x2e1e41=fs['createReadStream'](_0x14a4ae);var _0x4102b=fs[_0xde2a('0x11')](_0x44dddb);var _0x585409=crypto[_0xde2a('0x12')](_0x2c66d3[_0xde2a('0x8')],_0x57465c);_0x2e1e41['on']('data',function(_0x9abcce){var _0x4c3c6b=new Buffer(_0x585409['update'](_0x9abcce),_0xde2a('0x13'));_0x4102b[_0xde2a('0x14')](_0x4c3c6b);});_0x2e1e41['on'](_0xde2a('0x15'),function(){try{var _0x13a701=new Buffer(_0x585409[_0xde2a('0x16')](_0xde2a('0x13')),_0xde2a('0x13'));_0x4102b[_0xde2a('0x14')](_0x13a701);_0x4102b['end']();_0x4102b['on'](_0xde2a('0x17'),function(){return _0x281e86(_0x44dddb);});}catch(_0x2ecd10){fs[_0xde2a('0x18')](_0x44dddb);return _0x4632aa(_0x2ecd10);}});});};exports[_0xde2a('0x19')]=function(_0x2740b0,_0x556bb3,_0x17777e,_0x2d5903){return new BPromise(function(_0x4fc088,_0x396358){_0x2d5903=combineOptions(_0x2d5903);var _0x1035bb=new Buffer(_0x17777e);var _0x497b2d=fs[_0xde2a('0x1a')](_0x2740b0);var _0x16fa1a=fs[_0xde2a('0x11')](_0x556bb3);var _0x10f575=crypto[_0xde2a('0x1b')](_0x2d5903[_0xde2a('0x8')],_0x1035bb);_0x497b2d['on'](_0xde2a('0x1c'),function(_0x41bb39){var _0x373d1f=new Buffer(_0x10f575['update'](_0x41bb39),_0xde2a('0x13'));_0x16fa1a[_0xde2a('0x14')](_0x373d1f);});_0x497b2d['on']('end',function(){try{var _0x348e04=new Buffer(_0x10f575[_0xde2a('0x16')](_0xde2a('0x13')),_0xde2a('0x13'));_0x16fa1a[_0xde2a('0x14')](_0x348e04);_0x16fa1a[_0xde2a('0x15')]();_0x16fa1a['on']('close',function(){return _0x4fc088(_0x556bb3);});}catch(_0x21c512){fs[_0xde2a('0x18')](_0x556bb3);return _0x396358(_0x21c512);}});});};exports['encryptString']=function(_0x515bb4,_0x311c56,_0x104c9c){var _0x3ccfbd=_0x104c9c||stringOptions[_0xde2a('0x8')];var _0xc6ec4b=_0x311c56||stringOptions[_0xde2a('0x1d')];var _0x5dc3e6=crypto[_0xde2a('0x12')](_0x3ccfbd,_0xc6ec4b);var _0x474a86=_0x5dc3e6[_0xde2a('0x1e')](_0x515bb4,_0xde2a('0x1f'),'base64');_0x474a86+=_0x5dc3e6['final'](_0xde2a('0x20'));return _0x474a86;};exports['decryptString']=function(_0x94cf6f,_0x5eca71,_0x1ea076){var _0x3b3bee=_0x1ea076||stringOptions['algorithm'];var _0x53c595=_0x5eca71||stringOptions['secret'];var _0x125086=crypto['createDecipher'](_0x3b3bee,_0x53c595);var _0x57b279=_0x125086[_0xde2a('0x1e')](_0x94cf6f,_0xde2a('0x20'),_0xde2a('0x1f'));_0x57b279+=_0x125086['final'](_0xde2a('0x1f'));return _0x57b279;};