7c2b33755dde9fe278719281588141abc2ae7209
[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 _0x84cf=['lodash','../../config/logger','app','secrets','aes-256-ctr','aes192','encrypt','algorithm','join','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','filter','indexOf','hasOwnProperty','encryptFile','createWriteStream','createCipher','update','binary','end','final','write','close','unlink','createReadStream','data','encryptString','secret','utf8','base64','decryptString','createDecipher','crypto'];(function(_0x2dafbe,_0x5d04de){var _0x58a45a=function(_0x50ac96){while(--_0x50ac96){_0x2dafbe['push'](_0x2dafbe['shift']());}};_0x58a45a(++_0x5d04de);}(_0x84cf,0x1cd));var _0xf84c=function(_0x312e90,_0x47626f){_0x312e90=_0x312e90-0x0;var _0x21790b=_0x84cf[_0x312e90];return _0x21790b;};'use strict';var crypto=require(_0xf84c('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require(_0xf84c('0x1'));var config=require('../../config/environment');var logger=require(_0xf84c('0x2'))(_0xf84c('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xf84c('0x4')]['session'],'algorithm':_0xf84c('0x5')};const fileOptions={'algorithm':_0xf84c('0x6')};if(config[_0xf84c('0x7')]&&config[_0xf84c('0x7')][_0xf84c('0x8')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0xf84c('0x8')]);if(isSupported){fileOptions[_0xf84c('0x8')]=config[_0xf84c('0x7')][_0xf84c('0x8')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xf84c('0x9')](',\x20');logger['error'](config[_0xf84c('0x7')]['algorithm'],_0xf84c('0xa'),supportedAlgorithms);}}function isASupportedCipher(_0x2afbb7){return AES_SUPPORTED_RX[_0xf84c('0xb')](_0x2afbb7);}function getSupportedCiphers(){return crypto['getCiphers']()[_0xf84c('0xc')](isASupportedCipher);}function isAlgorithmSupported(_0x1d9941){return getSupportedCiphers()[_0xf84c('0xd')](_0x1d9941)>-0x1;}function combineOptions(_0x315699){var _0x33bc52={};for(var _0x4fc0ee in fileOptions){if(fileOptions[_0xf84c('0xe')](_0x4fc0ee)){_0x33bc52[_0x4fc0ee]=fileOptions[_0x4fc0ee];}}if(_0x315699){for(var _0x45bf90 in _0x315699){if(_0x315699[_0xf84c('0xe')](_0x45bf90)){_0x33bc52[_0x45bf90]=_0x315699[_0x45bf90];}}}return _0x33bc52;}exports[_0xf84c('0xf')]=function(_0x37c396,_0x18004a,_0x13883c,_0x2a81c3){return new BPromise(function(_0x4991da,_0x1d0625){_0x2a81c3=combineOptions(_0x2a81c3);var _0x166f4a=new Buffer(_0x13883c);var _0x5687b8=fs['createReadStream'](_0x37c396);var _0xff94dc=fs[_0xf84c('0x10')](_0x18004a);var _0x4c179d=crypto[_0xf84c('0x11')](_0x2a81c3[_0xf84c('0x8')],_0x166f4a);_0x5687b8['on']('data',function(_0x42b779){var _0x5e5515=new Buffer(_0x4c179d[_0xf84c('0x12')](_0x42b779),_0xf84c('0x13'));_0xff94dc['write'](_0x5e5515);});_0x5687b8['on'](_0xf84c('0x14'),function(){try{var _0x4c1e7c=new Buffer(_0x4c179d[_0xf84c('0x15')](_0xf84c('0x13')),_0xf84c('0x13'));_0xff94dc[_0xf84c('0x16')](_0x4c1e7c);_0xff94dc['end']();_0xff94dc['on'](_0xf84c('0x17'),function(){return _0x4991da(_0x18004a);});}catch(_0x5a661d){fs[_0xf84c('0x18')](_0x18004a);return _0x1d0625(_0x5a661d);}});});};exports['decryptFile']=function(_0x2a4236,_0x30a3d7,_0x5d39a2,_0x5c8279){return new BPromise(function(_0x52982c,_0x4aff99){_0x5c8279=combineOptions(_0x5c8279);var _0x3f937a=new Buffer(_0x5d39a2);var _0x4b2afe=fs[_0xf84c('0x19')](_0x2a4236);var _0x3721a2=fs[_0xf84c('0x10')](_0x30a3d7);var _0x517eb1=crypto['createDecipher'](_0x5c8279['algorithm'],_0x3f937a);_0x4b2afe['on'](_0xf84c('0x1a'),function(_0x348f04){var _0x1177d3=new Buffer(_0x517eb1[_0xf84c('0x12')](_0x348f04),_0xf84c('0x13'));_0x3721a2[_0xf84c('0x16')](_0x1177d3);});_0x4b2afe['on'](_0xf84c('0x14'),function(){try{var _0x43a60b=new Buffer(_0x517eb1[_0xf84c('0x15')]('binary'),_0xf84c('0x13'));_0x3721a2[_0xf84c('0x16')](_0x43a60b);_0x3721a2['end']();_0x3721a2['on']('close',function(){return _0x52982c(_0x30a3d7);});}catch(_0x48ee59){fs[_0xf84c('0x18')](_0x30a3d7);return _0x4aff99(_0x48ee59);}});});};exports[_0xf84c('0x1b')]=function(_0xfcc99d,_0x3d5c2f,_0x3b67ac){var _0x4ecc7d=_0x3b67ac||stringOptions[_0xf84c('0x8')];var _0x158735=_0x3d5c2f||stringOptions[_0xf84c('0x1c')];var _0x3a06d9=crypto[_0xf84c('0x11')](_0x4ecc7d,_0x158735);var _0x141cd0=_0x3a06d9['update'](_0xfcc99d,_0xf84c('0x1d'),_0xf84c('0x1e'));_0x141cd0+=_0x3a06d9[_0xf84c('0x15')](_0xf84c('0x1e'));return _0x141cd0;};exports[_0xf84c('0x1f')]=function(_0x4c5420,_0x1ed575,_0x1703cb){var _0x5440f2=_0x1703cb||stringOptions['algorithm'];var _0x8c47c9=_0x1ed575||stringOptions[_0xf84c('0x1c')];var _0x101b17=crypto[_0xf84c('0x20')](_0x5440f2,_0x8c47c9);var _0x56be48=_0x101b17['update'](_0x4c5420,_0xf84c('0x1e'),'utf8');_0x56be48+=_0x101b17['final'](_0xf84c('0x1d'));return _0x56be48;};