Built motion from commit 953226e6.|2.6.32
[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 _0xba9e=['close','unlink','createDecipher','final','encryptString','secret','utf8','base64','decryptString','crypto','lodash','../../config/environment','../../config/logger','app','secrets','session','aes-256-ctr','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','filter','indexOf','encryptFile','createReadStream','createWriteStream','createCipher','data','update','binary','end','write'];(function(_0x2c7618,_0x26be9c){var _0x5ce3f0=function(_0x1837b3){while(--_0x1837b3){_0x2c7618['push'](_0x2c7618['shift']());}};_0x5ce3f0(++_0x26be9c);}(_0xba9e,0x13b));var _0xeba9=function(_0x57314c,_0x16941d){_0x57314c=_0x57314c-0x0;var _0x281e89=_0xba9e[_0x57314c];return _0x281e89;};'use strict';var crypto=require(_0xeba9('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require(_0xeba9('0x1'));var config=require(_0xeba9('0x2'));var logger=require(_0xeba9('0x3'))(_0xeba9('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xeba9('0x5')][_0xeba9('0x6')],'algorithm':_0xeba9('0x7')};const fileOptions={'algorithm':_0xeba9('0x8')};if(config[_0xeba9('0x9')]&&config[_0xeba9('0x9')][_0xeba9('0xa')]){var isSupported=isAlgorithmSupported(config['encrypt'][_0xeba9('0xa')]);if(isSupported){fileOptions[_0xeba9('0xa')]=config['encrypt'][_0xeba9('0xa')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xeba9('0xb')](',\x20');logger[_0xeba9('0xc')](config['encrypt'][_0xeba9('0xa')],_0xeba9('0xd'),supportedAlgorithms);}}function isASupportedCipher(_0x18e4e0){return AES_SUPPORTED_RX['test'](_0x18e4e0);}function getSupportedCiphers(){return crypto['getCiphers']()[_0xeba9('0xe')](isASupportedCipher);}function isAlgorithmSupported(_0x5e75d2){return getSupportedCiphers()[_0xeba9('0xf')](_0x5e75d2)>-0x1;}function combineOptions(_0x3f37f3){var _0x198d6d={};for(var _0x2fd7c5 in fileOptions){if(fileOptions['hasOwnProperty'](_0x2fd7c5)){_0x198d6d[_0x2fd7c5]=fileOptions[_0x2fd7c5];}}if(_0x3f37f3){for(var _0x6e98f9 in _0x3f37f3){if(_0x3f37f3['hasOwnProperty'](_0x6e98f9)){_0x198d6d[_0x6e98f9]=_0x3f37f3[_0x6e98f9];}}}return _0x198d6d;}exports[_0xeba9('0x10')]=function(_0x3b50c8,_0x171b94,_0x2ed79f,_0x37edc4){return new BPromise(function(_0x2f8446,_0x3f8598){_0x37edc4=combineOptions(_0x37edc4);var _0x54503d=new Buffer(_0x2ed79f);var _0x13e6f6=fs[_0xeba9('0x11')](_0x3b50c8);var _0x573b2b=fs[_0xeba9('0x12')](_0x171b94);var _0x30babc=crypto[_0xeba9('0x13')](_0x37edc4['algorithm'],_0x54503d);_0x13e6f6['on'](_0xeba9('0x14'),function(_0x6c0c05){var _0x5f472d=new Buffer(_0x30babc[_0xeba9('0x15')](_0x6c0c05),_0xeba9('0x16'));_0x573b2b['write'](_0x5f472d);});_0x13e6f6['on'](_0xeba9('0x17'),function(){try{var _0x83b115=new Buffer(_0x30babc['final'](_0xeba9('0x16')),_0xeba9('0x16'));_0x573b2b[_0xeba9('0x18')](_0x83b115);_0x573b2b[_0xeba9('0x17')]();_0x573b2b['on'](_0xeba9('0x19'),function(){return _0x2f8446(_0x171b94);});}catch(_0x198114){fs[_0xeba9('0x1a')](_0x171b94);return _0x3f8598(_0x198114);}});});};exports['decryptFile']=function(_0x3a8a0a,_0x4ded60,_0x374d81,_0x2d38c6){return new BPromise(function(_0x31457e,_0x32a0c3){_0x2d38c6=combineOptions(_0x2d38c6);var _0x2b9546=new Buffer(_0x374d81);var _0x4c8305=fs[_0xeba9('0x11')](_0x3a8a0a);var _0x5787f0=fs[_0xeba9('0x12')](_0x4ded60);var _0x2a7186=crypto[_0xeba9('0x1b')](_0x2d38c6[_0xeba9('0xa')],_0x2b9546);_0x4c8305['on']('data',function(_0x256199){var _0x2400ce=new Buffer(_0x2a7186[_0xeba9('0x15')](_0x256199),'binary');_0x5787f0[_0xeba9('0x18')](_0x2400ce);});_0x4c8305['on'](_0xeba9('0x17'),function(){try{var _0x51f012=new Buffer(_0x2a7186[_0xeba9('0x1c')](_0xeba9('0x16')),_0xeba9('0x16'));_0x5787f0[_0xeba9('0x18')](_0x51f012);_0x5787f0['end']();_0x5787f0['on'](_0xeba9('0x19'),function(){return _0x31457e(_0x4ded60);});}catch(_0x5ebe52){fs[_0xeba9('0x1a')](_0x4ded60);return _0x32a0c3(_0x5ebe52);}});});};exports[_0xeba9('0x1d')]=function(_0x4de610,_0x51a37b,_0x42cd44){var _0x475174=_0x42cd44||stringOptions['algorithm'];var _0x3c04e5=_0x51a37b||stringOptions[_0xeba9('0x1e')];var _0x5277c8=crypto[_0xeba9('0x13')](_0x475174,_0x3c04e5);var _0xd132f5=_0x5277c8[_0xeba9('0x15')](_0x4de610,_0xeba9('0x1f'),_0xeba9('0x20'));_0xd132f5+=_0x5277c8[_0xeba9('0x1c')](_0xeba9('0x20'));return _0xd132f5;};exports[_0xeba9('0x21')]=function(_0x25c852,_0x4f3c4b,_0x35f3d4){var _0x5c8529=_0x35f3d4||stringOptions[_0xeba9('0xa')];var _0x5361e4=_0x4f3c4b||stringOptions['secret'];var _0x5092b9=crypto[_0xeba9('0x1b')](_0x5c8529,_0x5361e4);var _0xc1c5da=_0x5092b9['update'](_0x25c852,_0xeba9('0x20'),_0xeba9('0x1f'));_0xc1c5da+=_0x5092b9[_0xeba9('0x1c')]('utf8');return _0xc1c5da;};