bd6eef4d37e128cda84d98693b8d4f9f1f3d90ae
[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 _0x1fe6=['decryptString','createDecipher','crypto','bluebird','lodash','../../config/environment','app','secrets','session','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','filter','hasOwnProperty','encryptFile','createReadStream','createCipher','data','update','binary','final','write','end','close','unlink','createWriteStream','secret','utf8','base64'];(function(_0x21bc66,_0x42c06b){var _0x2a753e=function(_0xab249a){while(--_0xab249a){_0x21bc66['push'](_0x21bc66['shift']());}};_0x2a753e(++_0x42c06b);}(_0x1fe6,0x182));var _0x61fe=function(_0x35371d,_0x9c1afd){_0x35371d=_0x35371d-0x0;var _0x2e4ec2=_0x1fe6[_0x35371d];return _0x2e4ec2;};'use strict';var crypto=require(_0x61fe('0x0'));var fs=require('fs');var BPromise=require(_0x61fe('0x1'));var _=require(_0x61fe('0x2'));var config=require(_0x61fe('0x3'));var logger=require('../../config/logger')(_0x61fe('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x61fe('0x5')][_0x61fe('0x6')],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0x61fe('0x7')};if(config[_0x61fe('0x8')]&&config['encrypt']['algorithm']){var isSupported=isAlgorithmSupported(config['encrypt'][_0x61fe('0x9')]);if(isSupported){fileOptions[_0x61fe('0x9')]=config['encrypt']['algorithm'];}else{var supportedAlgorithms=getSupportedCiphers()[_0x61fe('0xa')](',\x20');logger[_0x61fe('0xb')](config[_0x61fe('0x8')]['algorithm'],_0x61fe('0xc'),supportedAlgorithms);}}function isASupportedCipher(_0x3fe8d8){return AES_SUPPORTED_RX['test'](_0x3fe8d8);}function getSupportedCiphers(){return crypto['getCiphers']()[_0x61fe('0xd')](isASupportedCipher);}function isAlgorithmSupported(_0x176694){return getSupportedCiphers()['indexOf'](_0x176694)>-0x1;}function combineOptions(_0x281699){var _0x47834b={};for(var _0x25a0aa in fileOptions){if(fileOptions['hasOwnProperty'](_0x25a0aa)){_0x47834b[_0x25a0aa]=fileOptions[_0x25a0aa];}}if(_0x281699){for(var _0x445605 in _0x281699){if(_0x281699[_0x61fe('0xe')](_0x445605)){_0x47834b[_0x445605]=_0x281699[_0x445605];}}}return _0x47834b;}exports[_0x61fe('0xf')]=function(_0x3177d4,_0x299db4,_0x12b265,_0x3a8ff7){return new BPromise(function(_0x10c231,_0x3a48b0){_0x3a8ff7=combineOptions(_0x3a8ff7);var _0x498066=new Buffer(_0x12b265);var _0x444673=fs[_0x61fe('0x10')](_0x3177d4);var _0x197795=fs['createWriteStream'](_0x299db4);var _0x1d5154=crypto[_0x61fe('0x11')](_0x3a8ff7['algorithm'],_0x498066);_0x444673['on'](_0x61fe('0x12'),function(_0x10e05c){var _0x305eed=new Buffer(_0x1d5154[_0x61fe('0x13')](_0x10e05c),_0x61fe('0x14'));_0x197795['write'](_0x305eed);});_0x444673['on']('end',function(){try{var _0x4fd20a=new Buffer(_0x1d5154[_0x61fe('0x15')](_0x61fe('0x14')),_0x61fe('0x14'));_0x197795[_0x61fe('0x16')](_0x4fd20a);_0x197795[_0x61fe('0x17')]();_0x197795['on'](_0x61fe('0x18'),function(){return _0x10c231(_0x299db4);});}catch(_0x52357f){fs[_0x61fe('0x19')](_0x299db4);return _0x3a48b0(_0x52357f);}});});};exports['decryptFile']=function(_0x99acc4,_0x47fb95,_0x537e77,_0x7ab8b2){return new BPromise(function(_0x370c65,_0x4b6f5f){_0x7ab8b2=combineOptions(_0x7ab8b2);var _0x5e958f=new Buffer(_0x537e77);var _0x4f9904=fs[_0x61fe('0x10')](_0x99acc4);var _0x29576f=fs[_0x61fe('0x1a')](_0x47fb95);var _0x3f7021=crypto['createDecipher'](_0x7ab8b2[_0x61fe('0x9')],_0x5e958f);_0x4f9904['on'](_0x61fe('0x12'),function(_0x330d83){var _0x5064bf=new Buffer(_0x3f7021['update'](_0x330d83),'binary');_0x29576f[_0x61fe('0x16')](_0x5064bf);});_0x4f9904['on'](_0x61fe('0x17'),function(){try{var _0x474673=new Buffer(_0x3f7021[_0x61fe('0x15')]('binary'),_0x61fe('0x14'));_0x29576f[_0x61fe('0x16')](_0x474673);_0x29576f['end']();_0x29576f['on'](_0x61fe('0x18'),function(){return _0x370c65(_0x47fb95);});}catch(_0xf43cdd){fs[_0x61fe('0x19')](_0x47fb95);return _0x4b6f5f(_0xf43cdd);}});});};exports['encryptString']=function(_0x56f112,_0x694031,_0x2ae755){var _0x38c8cc=_0x2ae755||stringOptions['algorithm'];var _0x655937=_0x694031||stringOptions[_0x61fe('0x1b')];var _0x4dabab=crypto[_0x61fe('0x11')](_0x38c8cc,_0x655937);var _0x24d9e6=_0x4dabab['update'](_0x56f112,_0x61fe('0x1c'),_0x61fe('0x1d'));_0x24d9e6+=_0x4dabab[_0x61fe('0x15')](_0x61fe('0x1d'));return _0x24d9e6;};exports[_0x61fe('0x1e')]=function(_0x51b929,_0x4a99fb,_0x2bc3ac){var _0x5316b1=_0x2bc3ac||stringOptions[_0x61fe('0x9')];var _0x35f92d=_0x4a99fb||stringOptions[_0x61fe('0x1b')];var _0x20d54b=crypto[_0x61fe('0x1f')](_0x5316b1,_0x35f92d);var _0x2a4343=_0x20d54b[_0x61fe('0x13')](_0x51b929,_0x61fe('0x1d'),_0x61fe('0x1c'));_0x2a4343+=_0x20d54b[_0x61fe('0x15')](_0x61fe('0x1c'));return _0x2a4343;};