Built motion from commit d1eab355.|2.6.28
[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 _0x1b6d=['createDecipher','update','final','end','close','encryptString','utf8','base64','decryptString','crypto','bluebird','lodash','../../config/environment','app','secrets','session','aes-256-ctr','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','getCiphers','filter','indexOf','hasOwnProperty','encryptFile','createReadStream','createCipher','data','binary','write','unlink','createWriteStream'];(function(_0x2da39b,_0x3faa2f){var _0x4d30dd=function(_0x1cebb8){while(--_0x1cebb8){_0x2da39b['push'](_0x2da39b['shift']());}};_0x4d30dd(++_0x3faa2f);}(_0x1b6d,0x1f3));var _0xd1b6=function(_0x4e676e,_0x2a7c6d){_0x4e676e=_0x4e676e-0x0;var _0x336a3c=_0x1b6d[_0x4e676e];return _0x336a3c;};'use strict';var crypto=require(_0xd1b6('0x0'));var fs=require('fs');var BPromise=require(_0xd1b6('0x1'));var _=require(_0xd1b6('0x2'));var config=require(_0xd1b6('0x3'));var logger=require('../../config/logger')(_0xd1b6('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xd1b6('0x5')][_0xd1b6('0x6')],'algorithm':_0xd1b6('0x7')};const fileOptions={'algorithm':_0xd1b6('0x8')};if(config[_0xd1b6('0x9')]&&config[_0xd1b6('0x9')][_0xd1b6('0xa')]){var isSupported=isAlgorithmSupported(config[_0xd1b6('0x9')][_0xd1b6('0xa')]);if(isSupported){fileOptions['algorithm']=config['encrypt'][_0xd1b6('0xa')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xd1b6('0xb')](',\x20');logger[_0xd1b6('0xc')](config[_0xd1b6('0x9')][_0xd1b6('0xa')],_0xd1b6('0xd'),supportedAlgorithms);}}function isASupportedCipher(_0x545f51){return AES_SUPPORTED_RX['test'](_0x545f51);}function getSupportedCiphers(){return crypto[_0xd1b6('0xe')]()[_0xd1b6('0xf')](isASupportedCipher);}function isAlgorithmSupported(_0x529db8){return getSupportedCiphers()[_0xd1b6('0x10')](_0x529db8)>-0x1;}function combineOptions(_0x21ec3b){var _0x56f0ca={};for(var _0x3d8e53 in fileOptions){if(fileOptions[_0xd1b6('0x11')](_0x3d8e53)){_0x56f0ca[_0x3d8e53]=fileOptions[_0x3d8e53];}}if(_0x21ec3b){for(var _0x350d91 in _0x21ec3b){if(_0x21ec3b[_0xd1b6('0x11')](_0x350d91)){_0x56f0ca[_0x350d91]=_0x21ec3b[_0x350d91];}}}return _0x56f0ca;}exports[_0xd1b6('0x12')]=function(_0x392837,_0x483106,_0x4b8381,_0x12ebe0){return new BPromise(function(_0x417ad3,_0x360008){_0x12ebe0=combineOptions(_0x12ebe0);var _0x25eed1=new Buffer(_0x4b8381);var _0x424de9=fs[_0xd1b6('0x13')](_0x392837);var _0x348242=fs['createWriteStream'](_0x483106);var _0x1a5195=crypto[_0xd1b6('0x14')](_0x12ebe0[_0xd1b6('0xa')],_0x25eed1);_0x424de9['on'](_0xd1b6('0x15'),function(_0x2460cf){var _0x55bdc3=new Buffer(_0x1a5195['update'](_0x2460cf),_0xd1b6('0x16'));_0x348242[_0xd1b6('0x17')](_0x55bdc3);});_0x424de9['on']('end',function(){try{var _0x35b298=new Buffer(_0x1a5195['final'](_0xd1b6('0x16')),_0xd1b6('0x16'));_0x348242['write'](_0x35b298);_0x348242['end']();_0x348242['on']('close',function(){return _0x417ad3(_0x483106);});}catch(_0x436056){fs[_0xd1b6('0x18')](_0x483106);return _0x360008(_0x436056);}});});};exports['decryptFile']=function(_0x22c7df,_0x27d6ad,_0x43c3c4,_0x13552c){return new BPromise(function(_0x3b89a4,_0x1753a3){_0x13552c=combineOptions(_0x13552c);var _0x46a193=new Buffer(_0x43c3c4);var _0x4e1623=fs[_0xd1b6('0x13')](_0x22c7df);var _0x154cec=fs[_0xd1b6('0x19')](_0x27d6ad);var _0x1bfa67=crypto[_0xd1b6('0x1a')](_0x13552c[_0xd1b6('0xa')],_0x46a193);_0x4e1623['on'](_0xd1b6('0x15'),function(_0xe1735d){var _0x245067=new Buffer(_0x1bfa67[_0xd1b6('0x1b')](_0xe1735d),_0xd1b6('0x16'));_0x154cec[_0xd1b6('0x17')](_0x245067);});_0x4e1623['on']('end',function(){try{var _0x47a18f=new Buffer(_0x1bfa67[_0xd1b6('0x1c')](_0xd1b6('0x16')),_0xd1b6('0x16'));_0x154cec[_0xd1b6('0x17')](_0x47a18f);_0x154cec[_0xd1b6('0x1d')]();_0x154cec['on'](_0xd1b6('0x1e'),function(){return _0x3b89a4(_0x27d6ad);});}catch(_0x4ec1ca){fs[_0xd1b6('0x18')](_0x27d6ad);return _0x1753a3(_0x4ec1ca);}});});};exports[_0xd1b6('0x1f')]=function(_0x58b0f0,_0x504609,_0x24cf16){var _0x582311=_0x24cf16||stringOptions[_0xd1b6('0xa')];var _0x3dffdc=_0x504609||stringOptions['secret'];var _0x683740=crypto[_0xd1b6('0x14')](_0x582311,_0x3dffdc);var _0x30dcfc=_0x683740[_0xd1b6('0x1b')](_0x58b0f0,_0xd1b6('0x20'),_0xd1b6('0x21'));_0x30dcfc+=_0x683740[_0xd1b6('0x1c')](_0xd1b6('0x21'));return _0x30dcfc;};exports[_0xd1b6('0x22')]=function(_0x3a6293,_0x40592f,_0x3c20ba){var _0x3bec16=_0x3c20ba||stringOptions[_0xd1b6('0xa')];var _0xb1e094=_0x40592f||stringOptions['secret'];var _0x8d7525=crypto[_0xd1b6('0x1a')](_0x3bec16,_0xb1e094);var _0x4471f6=_0x8d7525['update'](_0x3a6293,_0xd1b6('0x21'),_0xd1b6('0x20'));_0x4471f6+=_0x8d7525[_0xd1b6('0x1c')](_0xd1b6('0x20'));return _0x4471f6;};