Built motion from commit b5996064.|2.6.21
[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 _0x1905=['end','final','close','unlink','decryptFile','createDecipher','encryptString','secret','base64','utf8','crypto','bluebird','lodash','../../config/logger','secrets','aes-256-ctr','algorithm','encrypt','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','data','update','binary','write'];(function(_0x39519d,_0x3345e1){var _0x483149=function(_0xad7083){while(--_0xad7083){_0x39519d['push'](_0x39519d['shift']());}};_0x483149(++_0x3345e1);}(_0x1905,0xaf));var _0x5190=function(_0x2424ca,_0x120f28){_0x2424ca=_0x2424ca-0x0;var _0x7d577a=_0x1905[_0x2424ca];return _0x7d577a;};'use strict';var crypto=require(_0x5190('0x0'));var fs=require('fs');var BPromise=require(_0x5190('0x1'));var _=require(_0x5190('0x2'));var config=require('../../config/environment');var logger=require(_0x5190('0x3'))('app');const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x5190('0x4')]['session'],'algorithm':_0x5190('0x5')};const fileOptions={'algorithm':'aes192'};if(config['encrypt']&&config['encrypt'][_0x5190('0x6')]){var isSupported=isAlgorithmSupported(config[_0x5190('0x7')][_0x5190('0x6')]);if(isSupported){fileOptions[_0x5190('0x6')]=config['encrypt'][_0x5190('0x6')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x5190('0x8')](',\x20');logger[_0x5190('0x9')](config['encrypt']['algorithm'],_0x5190('0xa'),supportedAlgorithms);}}function isASupportedCipher(_0x1f6179){return AES_SUPPORTED_RX[_0x5190('0xb')](_0x1f6179);}function getSupportedCiphers(){return crypto[_0x5190('0xc')]()['filter'](isASupportedCipher);}function isAlgorithmSupported(_0x3fbd97){return getSupportedCiphers()[_0x5190('0xd')](_0x3fbd97)>-0x1;}function combineOptions(_0x4d5aa2){var _0x3779d4={};for(var _0x5b6efb in fileOptions){if(fileOptions[_0x5190('0xe')](_0x5b6efb)){_0x3779d4[_0x5b6efb]=fileOptions[_0x5b6efb];}}if(_0x4d5aa2){for(var _0x2b70f8 in _0x4d5aa2){if(_0x4d5aa2[_0x5190('0xe')](_0x2b70f8)){_0x3779d4[_0x2b70f8]=_0x4d5aa2[_0x2b70f8];}}}return _0x3779d4;}exports[_0x5190('0xf')]=function(_0x1e6803,_0x54353e,_0x4bb526,_0x334bc5){return new BPromise(function(_0xfb2faa,_0x1b590c){_0x334bc5=combineOptions(_0x334bc5);var _0x3bc522=new Buffer(_0x4bb526);var _0x2115c1=fs[_0x5190('0x10')](_0x1e6803);var _0x47ef92=fs[_0x5190('0x11')](_0x54353e);var _0x5e7850=crypto[_0x5190('0x12')](_0x334bc5['algorithm'],_0x3bc522);_0x2115c1['on'](_0x5190('0x13'),function(_0x4a02f3){var _0x2ea3c9=new Buffer(_0x5e7850[_0x5190('0x14')](_0x4a02f3),_0x5190('0x15'));_0x47ef92[_0x5190('0x16')](_0x2ea3c9);});_0x2115c1['on'](_0x5190('0x17'),function(){try{var _0x44ec25=new Buffer(_0x5e7850[_0x5190('0x18')](_0x5190('0x15')),_0x5190('0x15'));_0x47ef92[_0x5190('0x16')](_0x44ec25);_0x47ef92[_0x5190('0x17')]();_0x47ef92['on'](_0x5190('0x19'),function(){return _0xfb2faa(_0x54353e);});}catch(_0x143f86){fs[_0x5190('0x1a')](_0x54353e);return _0x1b590c(_0x143f86);}});});};exports[_0x5190('0x1b')]=function(_0x3f269a,_0x43dee3,_0x5f7426,_0x217742){return new BPromise(function(_0x4bbe37,_0x3adca8){_0x217742=combineOptions(_0x217742);var _0x1d8095=new Buffer(_0x5f7426);var _0x33659a=fs[_0x5190('0x10')](_0x3f269a);var _0x566422=fs[_0x5190('0x11')](_0x43dee3);var _0x3cd849=crypto[_0x5190('0x1c')](_0x217742[_0x5190('0x6')],_0x1d8095);_0x33659a['on'](_0x5190('0x13'),function(_0x5b5ee9){var _0x4cbbd0=new Buffer(_0x3cd849['update'](_0x5b5ee9),_0x5190('0x15'));_0x566422['write'](_0x4cbbd0);});_0x33659a['on'](_0x5190('0x17'),function(){try{var _0x219a98=new Buffer(_0x3cd849['final'](_0x5190('0x15')),_0x5190('0x15'));_0x566422['write'](_0x219a98);_0x566422[_0x5190('0x17')]();_0x566422['on'](_0x5190('0x19'),function(){return _0x4bbe37(_0x43dee3);});}catch(_0x190fad){fs[_0x5190('0x1a')](_0x43dee3);return _0x3adca8(_0x190fad);}});});};exports[_0x5190('0x1d')]=function(_0x40088e,_0x3369f7,_0x5811a6){var _0x49ac94=_0x5811a6||stringOptions['algorithm'];var _0x3fb333=_0x3369f7||stringOptions[_0x5190('0x1e')];var _0x2cdaa4=crypto['createCipher'](_0x49ac94,_0x3fb333);var _0xc74fcb=_0x2cdaa4['update'](_0x40088e,'utf8','base64');_0xc74fcb+=_0x2cdaa4['final'](_0x5190('0x1f'));return _0xc74fcb;};exports['decryptString']=function(_0x653016,_0x15665b,_0x1ac0d7){var _0x111896=_0x1ac0d7||stringOptions[_0x5190('0x6')];var _0x4c858c=_0x15665b||stringOptions[_0x5190('0x1e')];var _0x46a3f3=crypto[_0x5190('0x1c')](_0x111896,_0x4c858c);var _0x349f78=_0x46a3f3[_0x5190('0x14')](_0x653016,_0x5190('0x1f'),_0x5190('0x20'));_0x349f78+=_0x46a3f3[_0x5190('0x18')](_0x5190('0x20'));return _0x349f78;};