Built motion from commit e02f907f.|2.6.14
[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 _0xf25d=['decryptFile','createDecipher','data','encryptString','secret','utf8','base64','crypto','bluebird','lodash','../../config/environment','../../config/logger','app','secrets','session','aes-256-ctr','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','getCiphers','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','update','binary','write','end','final','close','unlink'];(function(_0x478d6a,_0x5e2a8d){var _0x2992de=function(_0x826280){while(--_0x826280){_0x478d6a['push'](_0x478d6a['shift']());}};_0x2992de(++_0x5e2a8d);}(_0xf25d,0x117));var _0xdf25=function(_0x58fd6c,_0x5a15d2){_0x58fd6c=_0x58fd6c-0x0;var _0xf979ab=_0xf25d[_0x58fd6c];return _0xf979ab;};'use strict';var crypto=require(_0xdf25('0x0'));var fs=require('fs');var BPromise=require(_0xdf25('0x1'));var _=require(_0xdf25('0x2'));var config=require(_0xdf25('0x3'));var logger=require(_0xdf25('0x4'))(_0xdf25('0x5'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xdf25('0x6')][_0xdf25('0x7')],'algorithm':_0xdf25('0x8')};const fileOptions={'algorithm':'aes192'};if(config[_0xdf25('0x9')]&&config['encrypt'][_0xdf25('0xa')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0xdf25('0xa')]);if(isSupported){fileOptions[_0xdf25('0xa')]=config['encrypt'][_0xdf25('0xa')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xdf25('0xb')](',\x20');logger[_0xdf25('0xc')](config[_0xdf25('0x9')][_0xdf25('0xa')],_0xdf25('0xd'),supportedAlgorithms);}}function isASupportedCipher(_0x3da7e0){return AES_SUPPORTED_RX['test'](_0x3da7e0);}function getSupportedCiphers(){return crypto[_0xdf25('0xe')]()['filter'](isASupportedCipher);}function isAlgorithmSupported(_0x325795){return getSupportedCiphers()['indexOf'](_0x325795)>-0x1;}function combineOptions(_0x2dd4f8){var _0x1351f1={};for(var _0x2c97af in fileOptions){if(fileOptions[_0xdf25('0xf')](_0x2c97af)){_0x1351f1[_0x2c97af]=fileOptions[_0x2c97af];}}if(_0x2dd4f8){for(var _0x166f51 in _0x2dd4f8){if(_0x2dd4f8['hasOwnProperty'](_0x166f51)){_0x1351f1[_0x166f51]=_0x2dd4f8[_0x166f51];}}}return _0x1351f1;}exports[_0xdf25('0x10')]=function(_0x549397,_0x194a92,_0x295d1c,_0x4e6898){return new BPromise(function(_0x4577a6,_0x4bc0b6){_0x4e6898=combineOptions(_0x4e6898);var _0x2af8dc=new Buffer(_0x295d1c);var _0x916aa3=fs[_0xdf25('0x11')](_0x549397);var _0x11ee1f=fs[_0xdf25('0x12')](_0x194a92);var _0x574510=crypto[_0xdf25('0x13')](_0x4e6898['algorithm'],_0x2af8dc);_0x916aa3['on']('data',function(_0x2b8089){var _0x188095=new Buffer(_0x574510[_0xdf25('0x14')](_0x2b8089),_0xdf25('0x15'));_0x11ee1f[_0xdf25('0x16')](_0x188095);});_0x916aa3['on'](_0xdf25('0x17'),function(){try{var _0x242e2c=new Buffer(_0x574510[_0xdf25('0x18')]('binary'),_0xdf25('0x15'));_0x11ee1f[_0xdf25('0x16')](_0x242e2c);_0x11ee1f[_0xdf25('0x17')]();_0x11ee1f['on'](_0xdf25('0x19'),function(){return _0x4577a6(_0x194a92);});}catch(_0x5f0858){fs[_0xdf25('0x1a')](_0x194a92);return _0x4bc0b6(_0x5f0858);}});});};exports[_0xdf25('0x1b')]=function(_0x15f00d,_0x1a306a,_0x1580bc,_0x3abdfc){return new BPromise(function(_0x5db700,_0xebe72e){_0x3abdfc=combineOptions(_0x3abdfc);var _0x4b534c=new Buffer(_0x1580bc);var _0x1992c4=fs[_0xdf25('0x11')](_0x15f00d);var _0x5234ce=fs['createWriteStream'](_0x1a306a);var _0x528896=crypto[_0xdf25('0x1c')](_0x3abdfc[_0xdf25('0xa')],_0x4b534c);_0x1992c4['on'](_0xdf25('0x1d'),function(_0x1b4557){var _0x3337cf=new Buffer(_0x528896['update'](_0x1b4557),_0xdf25('0x15'));_0x5234ce[_0xdf25('0x16')](_0x3337cf);});_0x1992c4['on'](_0xdf25('0x17'),function(){try{var _0x584ffc=new Buffer(_0x528896['final'](_0xdf25('0x15')),'binary');_0x5234ce[_0xdf25('0x16')](_0x584ffc);_0x5234ce[_0xdf25('0x17')]();_0x5234ce['on']('close',function(){return _0x5db700(_0x1a306a);});}catch(_0x36c762){fs[_0xdf25('0x1a')](_0x1a306a);return _0xebe72e(_0x36c762);}});});};exports[_0xdf25('0x1e')]=function(_0x4ae274,_0x462f1f,_0x2bca8a){var _0x36d0b2=_0x2bca8a||stringOptions[_0xdf25('0xa')];var _0x261fe6=_0x462f1f||stringOptions[_0xdf25('0x1f')];var _0x171976=crypto['createCipher'](_0x36d0b2,_0x261fe6);var _0x474840=_0x171976[_0xdf25('0x14')](_0x4ae274,_0xdf25('0x20'),_0xdf25('0x21'));_0x474840+=_0x171976['final']('base64');return _0x474840;};exports['decryptString']=function(_0x3815d8,_0x357729,_0x65c4b3){var _0x48d826=_0x65c4b3||stringOptions[_0xdf25('0xa')];var _0x11102d=_0x357729||stringOptions[_0xdf25('0x1f')];var _0x552a18=crypto['createDecipher'](_0x48d826,_0x11102d);var _0x494d67=_0x552a18['update'](_0x3815d8,_0xdf25('0x21'),_0xdf25('0x20'));_0x494d67+=_0x552a18[_0xdf25('0x18')](_0xdf25('0x20'));return _0x494d67;};