Built motion from commit 6db1674d.|2.6.23
[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 _0x49fb=['algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','filter','indexOf','hasOwnProperty','createWriteStream','createCipher','data','update','binary','write','end','close','createReadStream','createDecipher','unlink','encryptString','secret','utf8','base64','final','decryptString','lodash','../../config/environment','../../config/logger','app','secrets','session','encrypt'];(function(_0x3a4777,_0x173c1a){var _0x15dd7d=function(_0x24c4f3){while(--_0x24c4f3){_0x3a4777['push'](_0x3a4777['shift']());}};_0x15dd7d(++_0x173c1a);}(_0x49fb,0x164));var _0xb49f=function(_0x3ab51d,_0x2fa01d){_0x3ab51d=_0x3ab51d-0x0;var _0x29a04e=_0x49fb[_0x3ab51d];return _0x29a04e;};'use strict';var crypto=require('crypto');var fs=require('fs');var BPromise=require('bluebird');var _=require(_0xb49f('0x0'));var config=require(_0xb49f('0x1'));var logger=require(_0xb49f('0x2'))(_0xb49f('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xb49f('0x4')][_0xb49f('0x5')],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':'aes192'};if(config[_0xb49f('0x6')]&&config[_0xb49f('0x6')][_0xb49f('0x7')]){var isSupported=isAlgorithmSupported(config[_0xb49f('0x6')][_0xb49f('0x7')]);if(isSupported){fileOptions[_0xb49f('0x7')]=config[_0xb49f('0x6')][_0xb49f('0x7')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xb49f('0x8')](',\x20');logger[_0xb49f('0x9')](config['encrypt'][_0xb49f('0x7')],_0xb49f('0xa'),supportedAlgorithms);}}function isASupportedCipher(_0x12b5ff){return AES_SUPPORTED_RX[_0xb49f('0xb')](_0x12b5ff);}function getSupportedCiphers(){return crypto[_0xb49f('0xc')]()[_0xb49f('0xd')](isASupportedCipher);}function isAlgorithmSupported(_0x1bf58f){return getSupportedCiphers()[_0xb49f('0xe')](_0x1bf58f)>-0x1;}function combineOptions(_0x5cb615){var _0x142cfe={};for(var _0x503899 in fileOptions){if(fileOptions[_0xb49f('0xf')](_0x503899)){_0x142cfe[_0x503899]=fileOptions[_0x503899];}}if(_0x5cb615){for(var _0xa166a2 in _0x5cb615){if(_0x5cb615['hasOwnProperty'](_0xa166a2)){_0x142cfe[_0xa166a2]=_0x5cb615[_0xa166a2];}}}return _0x142cfe;}exports['encryptFile']=function(_0x5856aa,_0x478d1c,_0x2186bd,_0x536281){return new BPromise(function(_0x466cad,_0x50f325){_0x536281=combineOptions(_0x536281);var _0x4751cc=new Buffer(_0x2186bd);var _0x29cce9=fs['createReadStream'](_0x5856aa);var _0x20f97d=fs[_0xb49f('0x10')](_0x478d1c);var _0x2d0019=crypto[_0xb49f('0x11')](_0x536281[_0xb49f('0x7')],_0x4751cc);_0x29cce9['on'](_0xb49f('0x12'),function(_0x5b78a9){var _0x4624cb=new Buffer(_0x2d0019[_0xb49f('0x13')](_0x5b78a9),_0xb49f('0x14'));_0x20f97d[_0xb49f('0x15')](_0x4624cb);});_0x29cce9['on'](_0xb49f('0x16'),function(){try{var _0xc278f6=new Buffer(_0x2d0019['final'](_0xb49f('0x14')),_0xb49f('0x14'));_0x20f97d[_0xb49f('0x15')](_0xc278f6);_0x20f97d['end']();_0x20f97d['on'](_0xb49f('0x17'),function(){return _0x466cad(_0x478d1c);});}catch(_0x176b5a){fs['unlink'](_0x478d1c);return _0x50f325(_0x176b5a);}});});};exports['decryptFile']=function(_0x403aa6,_0x452c7b,_0xa78322,_0x3d56a8){return new BPromise(function(_0xffda68,_0x3fbb36){_0x3d56a8=combineOptions(_0x3d56a8);var _0x37acaf=new Buffer(_0xa78322);var _0x175c19=fs[_0xb49f('0x18')](_0x403aa6);var _0x490d8d=fs[_0xb49f('0x10')](_0x452c7b);var _0xa66bd=crypto[_0xb49f('0x19')](_0x3d56a8[_0xb49f('0x7')],_0x37acaf);_0x175c19['on'](_0xb49f('0x12'),function(_0x1646ad){var _0x30c2b1=new Buffer(_0xa66bd[_0xb49f('0x13')](_0x1646ad),_0xb49f('0x14'));_0x490d8d[_0xb49f('0x15')](_0x30c2b1);});_0x175c19['on'](_0xb49f('0x16'),function(){try{var _0x2d8b26=new Buffer(_0xa66bd['final'](_0xb49f('0x14')),_0xb49f('0x14'));_0x490d8d[_0xb49f('0x15')](_0x2d8b26);_0x490d8d[_0xb49f('0x16')]();_0x490d8d['on'](_0xb49f('0x17'),function(){return _0xffda68(_0x452c7b);});}catch(_0x43cfe9){fs[_0xb49f('0x1a')](_0x452c7b);return _0x3fbb36(_0x43cfe9);}});});};exports[_0xb49f('0x1b')]=function(_0x302b32,_0x33e1cb,_0x523a85){var _0x212d39=_0x523a85||stringOptions[_0xb49f('0x7')];var _0x116597=_0x33e1cb||stringOptions[_0xb49f('0x1c')];var _0x4a7a99=crypto['createCipher'](_0x212d39,_0x116597);var _0x2baae6=_0x4a7a99[_0xb49f('0x13')](_0x302b32,_0xb49f('0x1d'),_0xb49f('0x1e'));_0x2baae6+=_0x4a7a99[_0xb49f('0x1f')](_0xb49f('0x1e'));return _0x2baae6;};exports[_0xb49f('0x20')]=function(_0x330b4f,_0x145c00,_0xa8d424){var _0xe28725=_0xa8d424||stringOptions['algorithm'];var _0x340545=_0x145c00||stringOptions['secret'];var _0x23749e=crypto[_0xb49f('0x19')](_0xe28725,_0x340545);var _0x5489da=_0x23749e['update'](_0x330b4f,_0xb49f('0x1e'),_0xb49f('0x1d'));_0x5489da+=_0x23749e[_0xb49f('0x1f')](_0xb49f('0x1d'));return _0x5489da;};