Built motion from commit cb845558.|2.6.34
[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 _0xeaa7=['encrypt','algorithm','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','filter','indexOf','hasOwnProperty','encryptFile','createReadStream','createWriteStream','createCipher','data','write','end','final','binary','close','createDecipher','encryptString','secret','utf8','base64','decryptString','crypto','lodash','../../config/logger','app','secrets','session'];(function(_0x577ae7,_0x22cab4){var _0x223614=function(_0x5deedf){while(--_0x5deedf){_0x577ae7['push'](_0x577ae7['shift']());}};_0x223614(++_0x22cab4);}(_0xeaa7,0x14f));var _0x7eaa=function(_0x1034ca,_0x14ecd6){_0x1034ca=_0x1034ca-0x0;var _0x1a023a=_0xeaa7[_0x1034ca];return _0x1a023a;};'use strict';var crypto=require(_0x7eaa('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require(_0x7eaa('0x1'));var config=require('../../config/environment');var logger=require(_0x7eaa('0x2'))(_0x7eaa('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0x7eaa('0x4')][_0x7eaa('0x5')],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':'aes192'};if(config[_0x7eaa('0x6')]&&config[_0x7eaa('0x6')][_0x7eaa('0x7')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0x7eaa('0x7')]);if(isSupported){fileOptions['algorithm']=config[_0x7eaa('0x6')]['algorithm'];}else{var supportedAlgorithms=getSupportedCiphers()['join'](',\x20');logger[_0x7eaa('0x8')](config[_0x7eaa('0x6')][_0x7eaa('0x7')],_0x7eaa('0x9'),supportedAlgorithms);}}function isASupportedCipher(_0x21d7bd){return AES_SUPPORTED_RX[_0x7eaa('0xa')](_0x21d7bd);}function getSupportedCiphers(){return crypto[_0x7eaa('0xb')]()[_0x7eaa('0xc')](isASupportedCipher);}function isAlgorithmSupported(_0x4d300f){return getSupportedCiphers()[_0x7eaa('0xd')](_0x4d300f)>-0x1;}function combineOptions(_0x530ac7){var _0x320265={};for(var _0x16baad in fileOptions){if(fileOptions[_0x7eaa('0xe')](_0x16baad)){_0x320265[_0x16baad]=fileOptions[_0x16baad];}}if(_0x530ac7){for(var _0x5ea28a in _0x530ac7){if(_0x530ac7[_0x7eaa('0xe')](_0x5ea28a)){_0x320265[_0x5ea28a]=_0x530ac7[_0x5ea28a];}}}return _0x320265;}exports[_0x7eaa('0xf')]=function(_0x9067dc,_0x482a8e,_0xc0be3a,_0x340bca){return new BPromise(function(_0xf78ce3,_0x3c651e){_0x340bca=combineOptions(_0x340bca);var _0x3d56ec=new Buffer(_0xc0be3a);var _0x2c25c7=fs[_0x7eaa('0x10')](_0x9067dc);var _0x434b3e=fs[_0x7eaa('0x11')](_0x482a8e);var _0x167217=crypto[_0x7eaa('0x12')](_0x340bca[_0x7eaa('0x7')],_0x3d56ec);_0x2c25c7['on'](_0x7eaa('0x13'),function(_0x128c62){var _0x37b580=new Buffer(_0x167217['update'](_0x128c62),'binary');_0x434b3e[_0x7eaa('0x14')](_0x37b580);});_0x2c25c7['on'](_0x7eaa('0x15'),function(){try{var _0x3d7f12=new Buffer(_0x167217[_0x7eaa('0x16')](_0x7eaa('0x17')),_0x7eaa('0x17'));_0x434b3e[_0x7eaa('0x14')](_0x3d7f12);_0x434b3e['end']();_0x434b3e['on'](_0x7eaa('0x18'),function(){return _0xf78ce3(_0x482a8e);});}catch(_0x443d07){fs['unlink'](_0x482a8e);return _0x3c651e(_0x443d07);}});});};exports['decryptFile']=function(_0x598bf0,_0x37ef79,_0x37e21f,_0x12886e){return new BPromise(function(_0x2e5724,_0x23beb4){_0x12886e=combineOptions(_0x12886e);var _0x4c8af8=new Buffer(_0x37e21f);var _0x5c0a81=fs[_0x7eaa('0x10')](_0x598bf0);var _0xf47a61=fs['createWriteStream'](_0x37ef79);var _0x4e146b=crypto[_0x7eaa('0x19')](_0x12886e[_0x7eaa('0x7')],_0x4c8af8);_0x5c0a81['on'](_0x7eaa('0x13'),function(_0x423915){var _0x459183=new Buffer(_0x4e146b['update'](_0x423915),_0x7eaa('0x17'));_0xf47a61['write'](_0x459183);});_0x5c0a81['on'](_0x7eaa('0x15'),function(){try{var _0x51999b=new Buffer(_0x4e146b['final'](_0x7eaa('0x17')),_0x7eaa('0x17'));_0xf47a61[_0x7eaa('0x14')](_0x51999b);_0xf47a61[_0x7eaa('0x15')]();_0xf47a61['on']('close',function(){return _0x2e5724(_0x37ef79);});}catch(_0x5b45ae){fs['unlink'](_0x37ef79);return _0x23beb4(_0x5b45ae);}});});};exports[_0x7eaa('0x1a')]=function(_0x13b079,_0x40f11e,_0x2dc543){var _0x460b67=_0x2dc543||stringOptions[_0x7eaa('0x7')];var _0x2920de=_0x40f11e||stringOptions[_0x7eaa('0x1b')];var _0x1f709b=crypto[_0x7eaa('0x12')](_0x460b67,_0x2920de);var _0x528265=_0x1f709b['update'](_0x13b079,_0x7eaa('0x1c'),_0x7eaa('0x1d'));_0x528265+=_0x1f709b['final'](_0x7eaa('0x1d'));return _0x528265;};exports[_0x7eaa('0x1e')]=function(_0x2a7af2,_0x3a5a4d,_0x2a5709){var _0x72dfe6=_0x2a5709||stringOptions[_0x7eaa('0x7')];var _0x460e04=_0x3a5a4d||stringOptions[_0x7eaa('0x1b')];var _0x26c333=crypto['createDecipher'](_0x72dfe6,_0x460e04);var _0x2f4d4b=_0x26c333['update'](_0x2a7af2,_0x7eaa('0x1d'),_0x7eaa('0x1c'));_0x2f4d4b+=_0x26c333[_0x7eaa('0x16')](_0x7eaa('0x1c'));return _0x2f4d4b;};