7fcc484abff222edcbb1640d2c568d8f39a3b595
[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 _0x04b9=['secrets','session','aes192','encrypt','algorithm','join','error','test','getCiphers','filter','indexOf','hasOwnProperty','encryptFile','createCipher','update','binary','write','end','final','close','decryptFile','createReadStream','createDecipher','data','unlink','encryptString','secret','utf8','base64','decryptString','crypto','lodash','../../config/environment','../../config/logger','app'];(function(_0x339129,_0x86dcc6){var _0x1570df=function(_0x3b537e){while(--_0x3b537e){_0x339129['push'](_0x339129['shift']());}};_0x1570df(++_0x86dcc6);}(_0x04b9,0x64));var _0x904b=function(_0xd8ecbb,_0x31a373){_0xd8ecbb=_0xd8ecbb-0x0;var _0xf565a0=_0x04b9[_0xd8ecbb];return _0xf565a0;};'use strict';var crypto=require(_0x904b('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require(_0x904b('0x1'));var config=require(_0x904b('0x2'));var logger=require(_0x904b('0x3'))(_0x904b('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x904b('0x5')][_0x904b('0x6')],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0x904b('0x7')};if(config[_0x904b('0x8')]&&config[_0x904b('0x8')][_0x904b('0x9')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0x904b('0x9')]);if(isSupported){fileOptions[_0x904b('0x9')]=config[_0x904b('0x8')][_0x904b('0x9')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x904b('0xa')](',\x20');logger[_0x904b('0xb')](config[_0x904b('0x8')]['algorithm'],'is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:',supportedAlgorithms);}}function isASupportedCipher(_0x3a56ab){return AES_SUPPORTED_RX[_0x904b('0xc')](_0x3a56ab);}function getSupportedCiphers(){return crypto[_0x904b('0xd')]()[_0x904b('0xe')](isASupportedCipher);}function isAlgorithmSupported(_0x3c915a){return getSupportedCiphers()[_0x904b('0xf')](_0x3c915a)>-0x1;}function combineOptions(_0x55ceff){var _0x4bacb4={};for(var _0x376216 in fileOptions){if(fileOptions[_0x904b('0x10')](_0x376216)){_0x4bacb4[_0x376216]=fileOptions[_0x376216];}}if(_0x55ceff){for(var _0x57e1df in _0x55ceff){if(_0x55ceff[_0x904b('0x10')](_0x57e1df)){_0x4bacb4[_0x57e1df]=_0x55ceff[_0x57e1df];}}}return _0x4bacb4;}exports[_0x904b('0x11')]=function(_0x24a5c5,_0x54f6d4,_0x3f4875,_0x423704){return new BPromise(function(_0x4ab876,_0x4adf2a){_0x423704=combineOptions(_0x423704);var _0x1a37a4=new Buffer(_0x3f4875);var _0x4312ad=fs['createReadStream'](_0x24a5c5);var _0x1454d6=fs['createWriteStream'](_0x54f6d4);var _0x3f8e7b=crypto[_0x904b('0x12')](_0x423704[_0x904b('0x9')],_0x1a37a4);_0x4312ad['on']('data',function(_0x5f0ec3){var _0x3e62b0=new Buffer(_0x3f8e7b[_0x904b('0x13')](_0x5f0ec3),_0x904b('0x14'));_0x1454d6[_0x904b('0x15')](_0x3e62b0);});_0x4312ad['on'](_0x904b('0x16'),function(){try{var _0x535fc0=new Buffer(_0x3f8e7b[_0x904b('0x17')](_0x904b('0x14')),_0x904b('0x14'));_0x1454d6[_0x904b('0x15')](_0x535fc0);_0x1454d6['end']();_0x1454d6['on'](_0x904b('0x18'),function(){return _0x4ab876(_0x54f6d4);});}catch(_0x31a878){fs['unlink'](_0x54f6d4);return _0x4adf2a(_0x31a878);}});});};exports[_0x904b('0x19')]=function(_0x991d11,_0x2df57f,_0x5ee48e,_0x4007e2){return new BPromise(function(_0x3331af,_0x5a5cf7){_0x4007e2=combineOptions(_0x4007e2);var _0x40f38f=new Buffer(_0x5ee48e);var _0x1feb56=fs[_0x904b('0x1a')](_0x991d11);var _0x11561a=fs['createWriteStream'](_0x2df57f);var _0x291c56=crypto[_0x904b('0x1b')](_0x4007e2[_0x904b('0x9')],_0x40f38f);_0x1feb56['on'](_0x904b('0x1c'),function(_0x3443e8){var _0x24d5e2=new Buffer(_0x291c56[_0x904b('0x13')](_0x3443e8),_0x904b('0x14'));_0x11561a[_0x904b('0x15')](_0x24d5e2);});_0x1feb56['on'](_0x904b('0x16'),function(){try{var _0x2f2fc3=new Buffer(_0x291c56[_0x904b('0x17')]('binary'),_0x904b('0x14'));_0x11561a[_0x904b('0x15')](_0x2f2fc3);_0x11561a[_0x904b('0x16')]();_0x11561a['on'](_0x904b('0x18'),function(){return _0x3331af(_0x2df57f);});}catch(_0xdfa38f){fs[_0x904b('0x1d')](_0x2df57f);return _0x5a5cf7(_0xdfa38f);}});});};exports[_0x904b('0x1e')]=function(_0x56841c,_0x12fb9c,_0x5d4f87){var _0x56632a=_0x5d4f87||stringOptions[_0x904b('0x9')];var _0x239b19=_0x12fb9c||stringOptions[_0x904b('0x1f')];var _0x1c077e=crypto['createCipher'](_0x56632a,_0x239b19);var _0x727ba6=_0x1c077e[_0x904b('0x13')](_0x56841c,_0x904b('0x20'),_0x904b('0x21'));_0x727ba6+=_0x1c077e[_0x904b('0x17')](_0x904b('0x21'));return _0x727ba6;};exports[_0x904b('0x22')]=function(_0x4a6de5,_0x22e959,_0x5f5b6f){var _0x426583=_0x5f5b6f||stringOptions['algorithm'];var _0x15d8c4=_0x22e959||stringOptions[_0x904b('0x1f')];var _0x52bf16=crypto['createDecipher'](_0x426583,_0x15d8c4);var _0x404595=_0x52bf16['update'](_0x4a6de5,_0x904b('0x21'),'utf8');_0x404595+=_0x52bf16['final'](_0x904b('0x20'));return _0x404595;};