ded77b6ba48d111ef74b7f768527a89f3cb8073e
[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 _0xca42=['utf8','bluebird','lodash','../../config/environment','../../config/logger','app','secrets','aes-256-ctr','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','filter','indexOf','hasOwnProperty','createReadStream','update','write','end','final','binary','close','unlink','decryptFile','createWriteStream','encryptString','secret','createCipher','base64','decryptString'];(function(_0x1951ea,_0x48013c){var _0x2f29ad=function(_0x18330b){while(--_0x18330b){_0x1951ea['push'](_0x1951ea['shift']());}};_0x2f29ad(++_0x48013c);}(_0xca42,0x67));var _0x2ca4=function(_0x44fc0a,_0x1dfdac){_0x44fc0a=_0x44fc0a-0x0;var _0x475db2=_0xca42[_0x44fc0a];return _0x475db2;};'use strict';var crypto=require('crypto');var fs=require('fs');var BPromise=require(_0x2ca4('0x0'));var _=require(_0x2ca4('0x1'));var config=require(_0x2ca4('0x2'));var logger=require(_0x2ca4('0x3'))(_0x2ca4('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x2ca4('0x5')]['session'],'algorithm':_0x2ca4('0x6')};const fileOptions={'algorithm':_0x2ca4('0x7')};if(config[_0x2ca4('0x8')]&&config[_0x2ca4('0x8')][_0x2ca4('0x9')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0x2ca4('0x9')]);if(isSupported){fileOptions['algorithm']=config[_0x2ca4('0x8')]['algorithm'];}else{var supportedAlgorithms=getSupportedCiphers()[_0x2ca4('0xa')](',\x20');logger[_0x2ca4('0xb')](config[_0x2ca4('0x8')][_0x2ca4('0x9')],_0x2ca4('0xc'),supportedAlgorithms);}}function isASupportedCipher(_0x386de0){return AES_SUPPORTED_RX[_0x2ca4('0xd')](_0x386de0);}function getSupportedCiphers(){return crypto[_0x2ca4('0xe')]()[_0x2ca4('0xf')](isASupportedCipher);}function isAlgorithmSupported(_0x56458d){return getSupportedCiphers()[_0x2ca4('0x10')](_0x56458d)>-0x1;}function combineOptions(_0x118060){var _0x2f5fe1={};for(var _0x311546 in fileOptions){if(fileOptions[_0x2ca4('0x11')](_0x311546)){_0x2f5fe1[_0x311546]=fileOptions[_0x311546];}}if(_0x118060){for(var _0x311975 in _0x118060){if(_0x118060[_0x2ca4('0x11')](_0x311975)){_0x2f5fe1[_0x311975]=_0x118060[_0x311975];}}}return _0x2f5fe1;}exports['encryptFile']=function(_0x20b63a,_0x50a489,_0x4b3207,_0x3437ec){return new BPromise(function(_0x41ad2a,_0x9178e0){_0x3437ec=combineOptions(_0x3437ec);var _0x51296b=new Buffer(_0x4b3207);var _0x37f4b4=fs[_0x2ca4('0x12')](_0x20b63a);var _0xaa7c4=fs['createWriteStream'](_0x50a489);var _0x114923=crypto['createCipher'](_0x3437ec[_0x2ca4('0x9')],_0x51296b);_0x37f4b4['on']('data',function(_0x489fd4){var _0x27e3b2=new Buffer(_0x114923[_0x2ca4('0x13')](_0x489fd4),'binary');_0xaa7c4[_0x2ca4('0x14')](_0x27e3b2);});_0x37f4b4['on'](_0x2ca4('0x15'),function(){try{var _0x7558bc=new Buffer(_0x114923[_0x2ca4('0x16')](_0x2ca4('0x17')),_0x2ca4('0x17'));_0xaa7c4[_0x2ca4('0x14')](_0x7558bc);_0xaa7c4[_0x2ca4('0x15')]();_0xaa7c4['on'](_0x2ca4('0x18'),function(){return _0x41ad2a(_0x50a489);});}catch(_0x58f5a3){fs[_0x2ca4('0x19')](_0x50a489);return _0x9178e0(_0x58f5a3);}});});};exports[_0x2ca4('0x1a')]=function(_0x4463bc,_0xb1fc08,_0x38129e,_0x48bb96){return new BPromise(function(_0xde8c5e,_0x406322){_0x48bb96=combineOptions(_0x48bb96);var _0xd31719=new Buffer(_0x38129e);var _0x3e5772=fs[_0x2ca4('0x12')](_0x4463bc);var _0x3d99fe=fs[_0x2ca4('0x1b')](_0xb1fc08);var _0x111564=crypto['createDecipher'](_0x48bb96['algorithm'],_0xd31719);_0x3e5772['on']('data',function(_0xbb4ddc){var _0x4388e6=new Buffer(_0x111564[_0x2ca4('0x13')](_0xbb4ddc),_0x2ca4('0x17'));_0x3d99fe[_0x2ca4('0x14')](_0x4388e6);});_0x3e5772['on'](_0x2ca4('0x15'),function(){try{var _0x2ba4d4=new Buffer(_0x111564[_0x2ca4('0x16')](_0x2ca4('0x17')),'binary');_0x3d99fe[_0x2ca4('0x14')](_0x2ba4d4);_0x3d99fe[_0x2ca4('0x15')]();_0x3d99fe['on'](_0x2ca4('0x18'),function(){return _0xde8c5e(_0xb1fc08);});}catch(_0x2290b0){fs[_0x2ca4('0x19')](_0xb1fc08);return _0x406322(_0x2290b0);}});});};exports[_0x2ca4('0x1c')]=function(_0x1fb852,_0x5ca103,_0x1a7cec){var _0x992954=_0x1a7cec||stringOptions[_0x2ca4('0x9')];var _0x5a6ae4=_0x5ca103||stringOptions[_0x2ca4('0x1d')];var _0x1174ff=crypto[_0x2ca4('0x1e')](_0x992954,_0x5a6ae4);var _0x16cdfd=_0x1174ff[_0x2ca4('0x13')](_0x1fb852,'utf8','base64');_0x16cdfd+=_0x1174ff[_0x2ca4('0x16')](_0x2ca4('0x1f'));return _0x16cdfd;};exports[_0x2ca4('0x20')]=function(_0x91d70a,_0x59c07a,_0x14d5f9){var _0x245753=_0x14d5f9||stringOptions[_0x2ca4('0x9')];var _0x452808=_0x59c07a||stringOptions[_0x2ca4('0x1d')];var _0x1f9ac8=crypto['createDecipher'](_0x245753,_0x452808);var _0x1e6401=_0x1f9ac8[_0x2ca4('0x13')](_0x91d70a,_0x2ca4('0x1f'),_0x2ca4('0x21'));_0x1e6401+=_0x1f9ac8[_0x2ca4('0x16')](_0x2ca4('0x21'));return _0x1e6401;};