6cc11758f7f22384391a232cba35da2afeba8c11
[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 _0x696c=['update','binary','end','final','write','close','unlink','decryptFile','createReadStream','createDecipher','secret','utf8','base64','decryptString','crypto','lodash','../../config/logger','app','secrets','aes192','encrypt','algorithm','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','getCiphers','filter','indexOf','hasOwnProperty','createWriteStream','createCipher','data'];(function(_0x5c2cfc,_0x14612f){var _0x43f153=function(_0x323b2d){while(--_0x323b2d){_0x5c2cfc['push'](_0x5c2cfc['shift']());}};_0x43f153(++_0x14612f);}(_0x696c,0xc8));var _0xc696=function(_0x1d75f7,_0x139d5e){_0x1d75f7=_0x1d75f7-0x0;var _0x307f9a=_0x696c[_0x1d75f7];return _0x307f9a;};'use strict';var crypto=require(_0xc696('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require(_0xc696('0x1'));var config=require('../../config/environment');var logger=require(_0xc696('0x2'))(_0xc696('0x3'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xc696('0x4')]['session'],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0xc696('0x5')};if(config['encrypt']&&config[_0xc696('0x6')]['algorithm']){var isSupported=isAlgorithmSupported(config[_0xc696('0x6')][_0xc696('0x7')]);if(isSupported){fileOptions[_0xc696('0x7')]=config[_0xc696('0x6')][_0xc696('0x7')];}else{var supportedAlgorithms=getSupportedCiphers()['join'](',\x20');logger[_0xc696('0x8')](config[_0xc696('0x6')][_0xc696('0x7')],_0xc696('0x9'),supportedAlgorithms);}}function isASupportedCipher(_0x245acb){return AES_SUPPORTED_RX['test'](_0x245acb);}function getSupportedCiphers(){return crypto[_0xc696('0xa')]()[_0xc696('0xb')](isASupportedCipher);}function isAlgorithmSupported(_0x490035){return getSupportedCiphers()[_0xc696('0xc')](_0x490035)>-0x1;}function combineOptions(_0x1a84cc){var _0x2c0265={};for(var _0x44971d in fileOptions){if(fileOptions['hasOwnProperty'](_0x44971d)){_0x2c0265[_0x44971d]=fileOptions[_0x44971d];}}if(_0x1a84cc){for(var _0x4ae25d in _0x1a84cc){if(_0x1a84cc[_0xc696('0xd')](_0x4ae25d)){_0x2c0265[_0x4ae25d]=_0x1a84cc[_0x4ae25d];}}}return _0x2c0265;}exports['encryptFile']=function(_0x1de8fd,_0x3def07,_0x5bfa16,_0x453020){return new BPromise(function(_0x4d181e,_0x4cf514){_0x453020=combineOptions(_0x453020);var _0x354935=new Buffer(_0x5bfa16);var _0x14ee83=fs['createReadStream'](_0x1de8fd);var _0xf46ae=fs[_0xc696('0xe')](_0x3def07);var _0x26ec79=crypto[_0xc696('0xf')](_0x453020[_0xc696('0x7')],_0x354935);_0x14ee83['on'](_0xc696('0x10'),function(_0x975cad){var _0x5d9273=new Buffer(_0x26ec79[_0xc696('0x11')](_0x975cad),_0xc696('0x12'));_0xf46ae['write'](_0x5d9273);});_0x14ee83['on'](_0xc696('0x13'),function(){try{var _0x5351b6=new Buffer(_0x26ec79[_0xc696('0x14')](_0xc696('0x12')),_0xc696('0x12'));_0xf46ae[_0xc696('0x15')](_0x5351b6);_0xf46ae[_0xc696('0x13')]();_0xf46ae['on'](_0xc696('0x16'),function(){return _0x4d181e(_0x3def07);});}catch(_0x2d2c67){fs[_0xc696('0x17')](_0x3def07);return _0x4cf514(_0x2d2c67);}});});};exports[_0xc696('0x18')]=function(_0x3a74de,_0x18dd43,_0x24a9a0,_0x391e54){return new BPromise(function(_0x2be33a,_0x37f245){_0x391e54=combineOptions(_0x391e54);var _0x23bfca=new Buffer(_0x24a9a0);var _0x159a1b=fs[_0xc696('0x19')](_0x3a74de);var _0x10fe7f=fs[_0xc696('0xe')](_0x18dd43);var _0x5986c4=crypto[_0xc696('0x1a')](_0x391e54[_0xc696('0x7')],_0x23bfca);_0x159a1b['on'](_0xc696('0x10'),function(_0x5152d1){var _0x2d7676=new Buffer(_0x5986c4['update'](_0x5152d1),_0xc696('0x12'));_0x10fe7f[_0xc696('0x15')](_0x2d7676);});_0x159a1b['on'](_0xc696('0x13'),function(){try{var _0x400f8b=new Buffer(_0x5986c4['final'](_0xc696('0x12')),_0xc696('0x12'));_0x10fe7f[_0xc696('0x15')](_0x400f8b);_0x10fe7f['end']();_0x10fe7f['on'](_0xc696('0x16'),function(){return _0x2be33a(_0x18dd43);});}catch(_0x4a4feb){fs[_0xc696('0x17')](_0x18dd43);return _0x37f245(_0x4a4feb);}});});};exports['encryptString']=function(_0x23279d,_0xfcbdc7,_0x2accef){var _0x26372a=_0x2accef||stringOptions[_0xc696('0x7')];var _0x4afcbc=_0xfcbdc7||stringOptions[_0xc696('0x1b')];var _0x2b68d9=crypto['createCipher'](_0x26372a,_0x4afcbc);var _0x36e3ed=_0x2b68d9['update'](_0x23279d,_0xc696('0x1c'),_0xc696('0x1d'));_0x36e3ed+=_0x2b68d9['final'](_0xc696('0x1d'));return _0x36e3ed;};exports[_0xc696('0x1e')]=function(_0x1522ef,_0x129782,_0x796bc2){var _0x185baa=_0x796bc2||stringOptions[_0xc696('0x7')];var _0x7a1c2e=_0x129782||stringOptions[_0xc696('0x1b')];var _0x330535=crypto[_0xc696('0x1a')](_0x185baa,_0x7a1c2e);var _0x3823b7=_0x330535[_0xc696('0x11')](_0x1522ef,_0xc696('0x1d'),_0xc696('0x1c'));_0x3823b7+=_0x330535['final'](_0xc696('0x1c'));return _0x3823b7;};