fb4dcf3d22bf3df0c3cd1b842cc831eb394ac01e
[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 _0x796e=['createReadStream','createWriteStream','createCipher','update','binary','write','end','final','close','unlink','createDecipher','data','secret','utf8','base64','decryptString','crypto','../../config/environment','../../config/logger','app','secrets','aes-256-ctr','aes192','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','getCiphers','filter','hasOwnProperty','encryptFile'];(function(_0x3feb3f,_0x211c4){var _0x585ac8=function(_0x1b6999){while(--_0x1b6999){_0x3feb3f['push'](_0x3feb3f['shift']());}};_0x585ac8(++_0x211c4);}(_0x796e,0x1de));var _0xe796=function(_0x528d91,_0x36364e){_0x528d91=_0x528d91-0x0;var _0x26bd5a=_0x796e[_0x528d91];return _0x26bd5a;};'use strict';var crypto=require(_0xe796('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require('lodash');var config=require(_0xe796('0x1'));var logger=require(_0xe796('0x2'))(_0xe796('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xe796('0x4')]['session'],'algorithm':_0xe796('0x5')};const fileOptions={'algorithm':_0xe796('0x6')};if(config['encrypt']&&config[_0xe796('0x7')][_0xe796('0x8')]){var isSupported=isAlgorithmSupported(config[_0xe796('0x7')][_0xe796('0x8')]);if(isSupported){fileOptions[_0xe796('0x8')]=config['encrypt'][_0xe796('0x8')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xe796('0x9')](',\x20');logger[_0xe796('0xa')](config[_0xe796('0x7')][_0xe796('0x8')],_0xe796('0xb'),supportedAlgorithms);}}function isASupportedCipher(_0x772fb9){return AES_SUPPORTED_RX[_0xe796('0xc')](_0x772fb9);}function getSupportedCiphers(){return crypto[_0xe796('0xd')]()[_0xe796('0xe')](isASupportedCipher);}function isAlgorithmSupported(_0x5c02c9){return getSupportedCiphers()['indexOf'](_0x5c02c9)>-0x1;}function combineOptions(_0x2e9df3){var _0x12f8fa={};for(var _0x2cca5c in fileOptions){if(fileOptions[_0xe796('0xf')](_0x2cca5c)){_0x12f8fa[_0x2cca5c]=fileOptions[_0x2cca5c];}}if(_0x2e9df3){for(var _0x877967 in _0x2e9df3){if(_0x2e9df3['hasOwnProperty'](_0x877967)){_0x12f8fa[_0x877967]=_0x2e9df3[_0x877967];}}}return _0x12f8fa;}exports[_0xe796('0x10')]=function(_0x254bba,_0x57672e,_0x4aacf2,_0x5a7fb2){return new BPromise(function(_0x1b147c,_0xf1fbfc){_0x5a7fb2=combineOptions(_0x5a7fb2);var _0x3a27b3=new Buffer(_0x4aacf2);var _0x52b7e2=fs[_0xe796('0x11')](_0x254bba);var _0x158701=fs[_0xe796('0x12')](_0x57672e);var _0x5a83b3=crypto[_0xe796('0x13')](_0x5a7fb2['algorithm'],_0x3a27b3);_0x52b7e2['on']('data',function(_0x30587d){var _0x5eb412=new Buffer(_0x5a83b3[_0xe796('0x14')](_0x30587d),_0xe796('0x15'));_0x158701[_0xe796('0x16')](_0x5eb412);});_0x52b7e2['on'](_0xe796('0x17'),function(){try{var _0x1eff6d=new Buffer(_0x5a83b3[_0xe796('0x18')]('binary'),_0xe796('0x15'));_0x158701[_0xe796('0x16')](_0x1eff6d);_0x158701['end']();_0x158701['on'](_0xe796('0x19'),function(){return _0x1b147c(_0x57672e);});}catch(_0x499623){fs[_0xe796('0x1a')](_0x57672e);return _0xf1fbfc(_0x499623);}});});};exports['decryptFile']=function(_0x19685a,_0x3d5961,_0x4c1e24,_0xff8c61){return new BPromise(function(_0x39e3d8,_0x5b89f5){_0xff8c61=combineOptions(_0xff8c61);var _0x54dcf2=new Buffer(_0x4c1e24);var _0x373593=fs[_0xe796('0x11')](_0x19685a);var _0x36b7cf=fs[_0xe796('0x12')](_0x3d5961);var _0x263e69=crypto[_0xe796('0x1b')](_0xff8c61[_0xe796('0x8')],_0x54dcf2);_0x373593['on'](_0xe796('0x1c'),function(_0x252d0a){var _0x3fd386=new Buffer(_0x263e69[_0xe796('0x14')](_0x252d0a),_0xe796('0x15'));_0x36b7cf[_0xe796('0x16')](_0x3fd386);});_0x373593['on'](_0xe796('0x17'),function(){try{var _0x346c23=new Buffer(_0x263e69[_0xe796('0x18')](_0xe796('0x15')),_0xe796('0x15'));_0x36b7cf[_0xe796('0x16')](_0x346c23);_0x36b7cf[_0xe796('0x17')]();_0x36b7cf['on'](_0xe796('0x19'),function(){return _0x39e3d8(_0x3d5961);});}catch(_0x5326f6){fs[_0xe796('0x1a')](_0x3d5961);return _0x5b89f5(_0x5326f6);}});});};exports['encryptString']=function(_0x481a4d,_0x5c7fd8,_0x140e30){var _0x2c9590=_0x140e30||stringOptions[_0xe796('0x8')];var _0xf882e2=_0x5c7fd8||stringOptions[_0xe796('0x1d')];var _0x2e2e14=crypto[_0xe796('0x13')](_0x2c9590,_0xf882e2);var _0x2071d0=_0x2e2e14['update'](_0x481a4d,_0xe796('0x1e'),_0xe796('0x1f'));_0x2071d0+=_0x2e2e14[_0xe796('0x18')]('base64');return _0x2071d0;};exports[_0xe796('0x20')]=function(_0x566ed2,_0x4be475,_0x3d03d3){var _0x306eea=_0x3d03d3||stringOptions[_0xe796('0x8')];var _0x5c19da=_0x4be475||stringOptions[_0xe796('0x1d')];var _0x140121=crypto[_0xe796('0x1b')](_0x306eea,_0x5c19da);var _0x45a496=_0x140121[_0xe796('0x14')](_0x566ed2,_0xe796('0x1f'),_0xe796('0x1e'));_0x45a496+=_0x140121[_0xe796('0x18')](_0xe796('0x1e'));return _0x45a496;};