4ec45325b9561c035fe857fc5dd557b1ecd6f2e8
[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 _0xd1ff=['../../config/environment','../../config/logger','app','session','aes-256-ctr','encrypt','algorithm','join','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','getCiphers','filter','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','data','update','binary','final','write','close','unlink','decryptFile','createDecipher','end','secret','utf8','base64','bluebird','lodash'];(function(_0x1d8119,_0xc98f17){var _0x3e30f6=function(_0x2c7f3e){while(--_0x2c7f3e){_0x1d8119['push'](_0x1d8119['shift']());}};_0x3e30f6(++_0xc98f17);}(_0xd1ff,0xfe));var _0xfd1f=function(_0x342baa,_0x4a110f){_0x342baa=_0x342baa-0x0;var _0x1208f3=_0xd1ff[_0x342baa];return _0x1208f3;};'use strict';var crypto=require('crypto');var fs=require('fs');var BPromise=require(_0xfd1f('0x0'));var _=require(_0xfd1f('0x1'));var config=require(_0xfd1f('0x2'));var logger=require(_0xfd1f('0x3'))(_0xfd1f('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config['secrets'][_0xfd1f('0x5')],'algorithm':_0xfd1f('0x6')};const fileOptions={'algorithm':'aes192'};if(config['encrypt']&&config[_0xfd1f('0x7')]['algorithm']){var isSupported=isAlgorithmSupported(config['encrypt'][_0xfd1f('0x8')]);if(isSupported){fileOptions[_0xfd1f('0x8')]=config[_0xfd1f('0x7')][_0xfd1f('0x8')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xfd1f('0x9')](',\x20');logger['error'](config[_0xfd1f('0x7')][_0xfd1f('0x8')],_0xfd1f('0xa'),supportedAlgorithms);}}function isASupportedCipher(_0x51979a){return AES_SUPPORTED_RX['test'](_0x51979a);}function getSupportedCiphers(){return crypto[_0xfd1f('0xb')]()[_0xfd1f('0xc')](isASupportedCipher);}function isAlgorithmSupported(_0x2ef562){return getSupportedCiphers()[_0xfd1f('0xd')](_0x2ef562)>-0x1;}function combineOptions(_0x32baf1){var _0x161e3a={};for(var _0x23ad3d in fileOptions){if(fileOptions[_0xfd1f('0xe')](_0x23ad3d)){_0x161e3a[_0x23ad3d]=fileOptions[_0x23ad3d];}}if(_0x32baf1){for(var _0x225b2d in _0x32baf1){if(_0x32baf1['hasOwnProperty'](_0x225b2d)){_0x161e3a[_0x225b2d]=_0x32baf1[_0x225b2d];}}}return _0x161e3a;}exports[_0xfd1f('0xf')]=function(_0x4d92b3,_0x2572e5,_0x3ec81d,_0x29447b){return new BPromise(function(_0x244fbf,_0x270d91){_0x29447b=combineOptions(_0x29447b);var _0x1cb5b6=new Buffer(_0x3ec81d);var _0xa93a89=fs[_0xfd1f('0x10')](_0x4d92b3);var _0x1a89d0=fs[_0xfd1f('0x11')](_0x2572e5);var _0x2ed0ea=crypto[_0xfd1f('0x12')](_0x29447b[_0xfd1f('0x8')],_0x1cb5b6);_0xa93a89['on'](_0xfd1f('0x13'),function(_0x3329c5){var _0x281989=new Buffer(_0x2ed0ea[_0xfd1f('0x14')](_0x3329c5),_0xfd1f('0x15'));_0x1a89d0['write'](_0x281989);});_0xa93a89['on']('end',function(){try{var _0x29e79e=new Buffer(_0x2ed0ea[_0xfd1f('0x16')](_0xfd1f('0x15')),_0xfd1f('0x15'));_0x1a89d0[_0xfd1f('0x17')](_0x29e79e);_0x1a89d0['end']();_0x1a89d0['on'](_0xfd1f('0x18'),function(){return _0x244fbf(_0x2572e5);});}catch(_0x3cadf5){fs[_0xfd1f('0x19')](_0x2572e5);return _0x270d91(_0x3cadf5);}});});};exports[_0xfd1f('0x1a')]=function(_0x4aac87,_0xac567c,_0x4d08a9,_0x12f39c){return new BPromise(function(_0x2ccc46,_0x5b57d5){_0x12f39c=combineOptions(_0x12f39c);var _0x1de2bb=new Buffer(_0x4d08a9);var _0x38ef4f=fs['createReadStream'](_0x4aac87);var _0x4d0f0d=fs[_0xfd1f('0x11')](_0xac567c);var _0x237cc5=crypto[_0xfd1f('0x1b')](_0x12f39c[_0xfd1f('0x8')],_0x1de2bb);_0x38ef4f['on'](_0xfd1f('0x13'),function(_0x2b7ef4){var _0x3844b6=new Buffer(_0x237cc5[_0xfd1f('0x14')](_0x2b7ef4),_0xfd1f('0x15'));_0x4d0f0d[_0xfd1f('0x17')](_0x3844b6);});_0x38ef4f['on'](_0xfd1f('0x1c'),function(){try{var _0x139004=new Buffer(_0x237cc5['final'](_0xfd1f('0x15')),_0xfd1f('0x15'));_0x4d0f0d[_0xfd1f('0x17')](_0x139004);_0x4d0f0d[_0xfd1f('0x1c')]();_0x4d0f0d['on'](_0xfd1f('0x18'),function(){return _0x2ccc46(_0xac567c);});}catch(_0x100065){fs[_0xfd1f('0x19')](_0xac567c);return _0x5b57d5(_0x100065);}});});};exports['encryptString']=function(_0x20d2cd,_0x106779,_0x49383a){var _0x9306be=_0x49383a||stringOptions[_0xfd1f('0x8')];var _0x196512=_0x106779||stringOptions[_0xfd1f('0x1d')];var _0x139373=crypto[_0xfd1f('0x12')](_0x9306be,_0x196512);var _0x47d4da=_0x139373[_0xfd1f('0x14')](_0x20d2cd,_0xfd1f('0x1e'),'base64');_0x47d4da+=_0x139373[_0xfd1f('0x16')](_0xfd1f('0x1f'));return _0x47d4da;};exports['decryptString']=function(_0x5f8687,_0x5c5c88,_0x1f0010){var _0x4b12fe=_0x1f0010||stringOptions['algorithm'];var _0x251a58=_0x5c5c88||stringOptions[_0xfd1f('0x1d')];var _0x6a89a8=crypto[_0xfd1f('0x1b')](_0x4b12fe,_0x251a58);var _0x222fef=_0x6a89a8[_0xfd1f('0x14')](_0x5f8687,_0xfd1f('0x1f'),'utf8');_0x222fef+=_0x6a89a8[_0xfd1f('0x16')](_0xfd1f('0x1e'));return _0x222fef;};