3253b6e661bbef68b1ee90168891c819e9a281c2
[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 _0xd399=['secrets','session','aes-256-ctr','aes192','encrypt','algorithm','join','test','filter','indexOf','hasOwnProperty','createWriteStream','update','binary','final','write','end','close','decryptFile','createReadStream','createDecipher','data','encryptString','secret','createCipher','base64','decryptString','crypto','bluebird','lodash','../../config/environment','../../config/logger','app'];(function(_0x4fa0e8,_0x26b636){var _0x19cadd=function(_0x4cda62){while(--_0x4cda62){_0x4fa0e8['push'](_0x4fa0e8['shift']());}};_0x19cadd(++_0x26b636);}(_0xd399,0x1a7));var _0x9d39=function(_0x5e6682,_0x2c8139){_0x5e6682=_0x5e6682-0x0;var _0x5f3810=_0xd399[_0x5e6682];return _0x5f3810;};'use strict';var crypto=require(_0x9d39('0x0'));var fs=require('fs');var BPromise=require(_0x9d39('0x1'));var _=require(_0x9d39('0x2'));var config=require(_0x9d39('0x3'));var logger=require(_0x9d39('0x4'))(_0x9d39('0x5'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x9d39('0x6')][_0x9d39('0x7')],'algorithm':_0x9d39('0x8')};const fileOptions={'algorithm':_0x9d39('0x9')};if(config[_0x9d39('0xa')]&&config['encrypt'][_0x9d39('0xb')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0x9d39('0xb')]);if(isSupported){fileOptions[_0x9d39('0xb')]=config[_0x9d39('0xa')][_0x9d39('0xb')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x9d39('0xc')](',\x20');logger['error'](config[_0x9d39('0xa')][_0x9d39('0xb')],'is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:',supportedAlgorithms);}}function isASupportedCipher(_0x27e2ed){return AES_SUPPORTED_RX[_0x9d39('0xd')](_0x27e2ed);}function getSupportedCiphers(){return crypto['getCiphers']()[_0x9d39('0xe')](isASupportedCipher);}function isAlgorithmSupported(_0x557a8e){return getSupportedCiphers()[_0x9d39('0xf')](_0x557a8e)>-0x1;}function combineOptions(_0xc36308){var _0x1e1538={};for(var _0x3a1ce1 in fileOptions){if(fileOptions[_0x9d39('0x10')](_0x3a1ce1)){_0x1e1538[_0x3a1ce1]=fileOptions[_0x3a1ce1];}}if(_0xc36308){for(var _0x9143d0 in _0xc36308){if(_0xc36308['hasOwnProperty'](_0x9143d0)){_0x1e1538[_0x9143d0]=_0xc36308[_0x9143d0];}}}return _0x1e1538;}exports['encryptFile']=function(_0x157237,_0x489dc0,_0x230873,_0x20ba9e){return new BPromise(function(_0x407182,_0x5e4e38){_0x20ba9e=combineOptions(_0x20ba9e);var _0x4bd38c=new Buffer(_0x230873);var _0x5bdeed=fs['createReadStream'](_0x157237);var _0x405b14=fs[_0x9d39('0x11')](_0x489dc0);var _0x41d643=crypto['createCipher'](_0x20ba9e[_0x9d39('0xb')],_0x4bd38c);_0x5bdeed['on']('data',function(_0x4e8a45){var _0x5bc650=new Buffer(_0x41d643[_0x9d39('0x12')](_0x4e8a45),_0x9d39('0x13'));_0x405b14['write'](_0x5bc650);});_0x5bdeed['on']('end',function(){try{var _0xc730c2=new Buffer(_0x41d643[_0x9d39('0x14')](_0x9d39('0x13')),'binary');_0x405b14[_0x9d39('0x15')](_0xc730c2);_0x405b14[_0x9d39('0x16')]();_0x405b14['on'](_0x9d39('0x17'),function(){return _0x407182(_0x489dc0);});}catch(_0x5b226e){fs['unlink'](_0x489dc0);return _0x5e4e38(_0x5b226e);}});});};exports[_0x9d39('0x18')]=function(_0x5d261f,_0x10fcd0,_0x9b5940,_0x1b0895){return new BPromise(function(_0x5d2698,_0x36c1d2){_0x1b0895=combineOptions(_0x1b0895);var _0x2cc3de=new Buffer(_0x9b5940);var _0x3604d7=fs[_0x9d39('0x19')](_0x5d261f);var _0x387f0e=fs[_0x9d39('0x11')](_0x10fcd0);var _0x8d5cec=crypto[_0x9d39('0x1a')](_0x1b0895['algorithm'],_0x2cc3de);_0x3604d7['on'](_0x9d39('0x1b'),function(_0x99fd60){var _0x586793=new Buffer(_0x8d5cec[_0x9d39('0x12')](_0x99fd60),_0x9d39('0x13'));_0x387f0e[_0x9d39('0x15')](_0x586793);});_0x3604d7['on'](_0x9d39('0x16'),function(){try{var _0xa3d27c=new Buffer(_0x8d5cec[_0x9d39('0x14')](_0x9d39('0x13')),_0x9d39('0x13'));_0x387f0e['write'](_0xa3d27c);_0x387f0e[_0x9d39('0x16')]();_0x387f0e['on'](_0x9d39('0x17'),function(){return _0x5d2698(_0x10fcd0);});}catch(_0x14c4e4){fs['unlink'](_0x10fcd0);return _0x36c1d2(_0x14c4e4);}});});};exports[_0x9d39('0x1c')]=function(_0x5af85d,_0x302e09,_0x39f52c){var _0x54238c=_0x39f52c||stringOptions['algorithm'];var _0x3851b5=_0x302e09||stringOptions[_0x9d39('0x1d')];var _0x8e494c=crypto[_0x9d39('0x1e')](_0x54238c,_0x3851b5);var _0x3d3e4e=_0x8e494c[_0x9d39('0x12')](_0x5af85d,'utf8','base64');_0x3d3e4e+=_0x8e494c[_0x9d39('0x14')](_0x9d39('0x1f'));return _0x3d3e4e;};exports[_0x9d39('0x20')]=function(_0x332edc,_0xb4eb9c,_0x1501a9){var _0x218599=_0x1501a9||stringOptions['algorithm'];var _0x1e1c2c=_0xb4eb9c||stringOptions[_0x9d39('0x1d')];var _0x4edefe=crypto[_0x9d39('0x1a')](_0x218599,_0x1e1c2c);var _0x49af3c=_0x4edefe['update'](_0x332edc,'base64','utf8');_0x49af3c+=_0x4edefe[_0x9d39('0x14')]('utf8');return _0x49af3c;};