2038da2c37edc98ba2e264f306909103d77ad476
[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 _0x8a93=['../../config/environment','../../config/logger','app','secrets','session','aes-256-ctr','aes192','encrypt','algorithm','join','error','test','getCiphers','indexOf','hasOwnProperty','createReadStream','data','update','binary','write','end','final','unlink','decryptFile','createWriteStream','close','encryptString','createCipher','utf8','base64','decryptString','secret','createDecipher','crypto','bluebird','lodash'];(function(_0xa1e3e3,_0x29fe0e){var _0xc980e9=function(_0x1f5adc){while(--_0x1f5adc){_0xa1e3e3['push'](_0xa1e3e3['shift']());}};_0xc980e9(++_0x29fe0e);}(_0x8a93,0x141));var _0x38a9=function(_0xf27a5d,_0x5a3b2f){_0xf27a5d=_0xf27a5d-0x0;var _0x217da7=_0x8a93[_0xf27a5d];return _0x217da7;};'use strict';var crypto=require(_0x38a9('0x0'));var fs=require('fs');var BPromise=require(_0x38a9('0x1'));var _=require(_0x38a9('0x2'));var config=require(_0x38a9('0x3'));var logger=require(_0x38a9('0x4'))(_0x38a9('0x5'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x38a9('0x6')][_0x38a9('0x7')],'algorithm':_0x38a9('0x8')};const fileOptions={'algorithm':_0x38a9('0x9')};if(config[_0x38a9('0xa')]&&config[_0x38a9('0xa')][_0x38a9('0xb')]){var isSupported=isAlgorithmSupported(config[_0x38a9('0xa')][_0x38a9('0xb')]);if(isSupported){fileOptions[_0x38a9('0xb')]=config[_0x38a9('0xa')][_0x38a9('0xb')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x38a9('0xc')](',\x20');logger[_0x38a9('0xd')](config[_0x38a9('0xa')][_0x38a9('0xb')],'is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:',supportedAlgorithms);}}function isASupportedCipher(_0x36d65a){return AES_SUPPORTED_RX[_0x38a9('0xe')](_0x36d65a);}function getSupportedCiphers(){return crypto[_0x38a9('0xf')]()['filter'](isASupportedCipher);}function isAlgorithmSupported(_0x13deae){return getSupportedCiphers()[_0x38a9('0x10')](_0x13deae)>-0x1;}function combineOptions(_0x119059){var _0x2ecc19={};for(var _0x1c94ca in fileOptions){if(fileOptions[_0x38a9('0x11')](_0x1c94ca)){_0x2ecc19[_0x1c94ca]=fileOptions[_0x1c94ca];}}if(_0x119059){for(var _0x6edaf7 in _0x119059){if(_0x119059[_0x38a9('0x11')](_0x6edaf7)){_0x2ecc19[_0x6edaf7]=_0x119059[_0x6edaf7];}}}return _0x2ecc19;}exports['encryptFile']=function(_0xc068e7,_0x48384a,_0x5d6cca,_0x4b4cb2){return new BPromise(function(_0x3f5edf,_0xb58728){_0x4b4cb2=combineOptions(_0x4b4cb2);var _0x5109b7=new Buffer(_0x5d6cca);var _0x252f96=fs[_0x38a9('0x12')](_0xc068e7);var _0x43f37e=fs['createWriteStream'](_0x48384a);var _0x3ac759=crypto['createCipher'](_0x4b4cb2['algorithm'],_0x5109b7);_0x252f96['on'](_0x38a9('0x13'),function(_0x1831a1){var _0x5c56d2=new Buffer(_0x3ac759[_0x38a9('0x14')](_0x1831a1),_0x38a9('0x15'));_0x43f37e[_0x38a9('0x16')](_0x5c56d2);});_0x252f96['on'](_0x38a9('0x17'),function(){try{var _0x5bbe78=new Buffer(_0x3ac759[_0x38a9('0x18')](_0x38a9('0x15')),'binary');_0x43f37e[_0x38a9('0x16')](_0x5bbe78);_0x43f37e['end']();_0x43f37e['on']('close',function(){return _0x3f5edf(_0x48384a);});}catch(_0x2d69fb){fs[_0x38a9('0x19')](_0x48384a);return _0xb58728(_0x2d69fb);}});});};exports[_0x38a9('0x1a')]=function(_0x390ff7,_0x40b340,_0x5e12b5,_0x44776b){return new BPromise(function(_0x332a45,_0x569bfa){_0x44776b=combineOptions(_0x44776b);var _0xc21d87=new Buffer(_0x5e12b5);var _0x43b077=fs[_0x38a9('0x12')](_0x390ff7);var _0x2ed7b1=fs[_0x38a9('0x1b')](_0x40b340);var _0xb9d2b9=crypto['createDecipher'](_0x44776b[_0x38a9('0xb')],_0xc21d87);_0x43b077['on'](_0x38a9('0x13'),function(_0x4dd224){var _0x22ad96=new Buffer(_0xb9d2b9[_0x38a9('0x14')](_0x4dd224),_0x38a9('0x15'));_0x2ed7b1[_0x38a9('0x16')](_0x22ad96);});_0x43b077['on'](_0x38a9('0x17'),function(){try{var _0x26a17a=new Buffer(_0xb9d2b9[_0x38a9('0x18')](_0x38a9('0x15')),_0x38a9('0x15'));_0x2ed7b1[_0x38a9('0x16')](_0x26a17a);_0x2ed7b1[_0x38a9('0x17')]();_0x2ed7b1['on'](_0x38a9('0x1c'),function(){return _0x332a45(_0x40b340);});}catch(_0x11e260){fs['unlink'](_0x40b340);return _0x569bfa(_0x11e260);}});});};exports[_0x38a9('0x1d')]=function(_0x2f2669,_0x34027f,_0x469f0a){var _0xe18df6=_0x469f0a||stringOptions[_0x38a9('0xb')];var _0x144429=_0x34027f||stringOptions['secret'];var _0x115342=crypto[_0x38a9('0x1e')](_0xe18df6,_0x144429);var _0x11c18a=_0x115342[_0x38a9('0x14')](_0x2f2669,_0x38a9('0x1f'),_0x38a9('0x20'));_0x11c18a+=_0x115342[_0x38a9('0x18')](_0x38a9('0x20'));return _0x11c18a;};exports[_0x38a9('0x21')]=function(_0x5c8ed7,_0x131d61,_0x2164f2){var _0x515140=_0x2164f2||stringOptions['algorithm'];var _0x11eaf2=_0x131d61||stringOptions[_0x38a9('0x22')];var _0x7b3843=crypto[_0x38a9('0x23')](_0x515140,_0x11eaf2);var _0x4b8004=_0x7b3843[_0x38a9('0x14')](_0x5c8ed7,_0x38a9('0x20'),_0x38a9('0x1f'));_0x4b8004+=_0x7b3843[_0x38a9('0x18')](_0x38a9('0x1f'));return _0x4b8004;};