Built motion from commit 449495f6.|2.6.27
[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 _0x276c=['update','encryptString','secret','utf8','base64','decryptString','crypto','lodash','app','secrets','session','aes192','encrypt','algorithm','join','filter','indexOf','hasOwnProperty','encryptFile','createWriteStream','createCipher','binary','write','end','final','close','unlink','decryptFile','createReadStream','createDecipher','data'];(function(_0x53e937,_0x5d671b){var _0x35efed=function(_0x1c4bf8){while(--_0x1c4bf8){_0x53e937['push'](_0x53e937['shift']());}};_0x35efed(++_0x5d671b);}(_0x276c,0x17a));var _0xc276=function(_0x14499f,_0x16a1c1){_0x14499f=_0x14499f-0x0;var _0x26aa9e=_0x276c[_0x14499f];return _0x26aa9e;};'use strict';var crypto=require(_0xc276('0x0'));var fs=require('fs');var BPromise=require('bluebird');var _=require(_0xc276('0x1'));var config=require('../../config/environment');var logger=require('../../config/logger')(_0xc276('0x2'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config[_0xc276('0x3')][_0xc276('0x4')],'algorithm':'aes-256-ctr'};const fileOptions={'algorithm':_0xc276('0x5')};if(config['encrypt']&&config[_0xc276('0x6')][_0xc276('0x7')]){var isSupported=isAlgorithmSupported(config[_0xc276('0x6')][_0xc276('0x7')]);if(isSupported){fileOptions[_0xc276('0x7')]=config['encrypt'][_0xc276('0x7')];}else{var supportedAlgorithms=getSupportedCiphers()[_0xc276('0x8')](',\x20');logger['error'](config['encrypt'][_0xc276('0x7')],'is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:',supportedAlgorithms);}}function isASupportedCipher(_0x15f1bb){return AES_SUPPORTED_RX['test'](_0x15f1bb);}function getSupportedCiphers(){return crypto['getCiphers']()[_0xc276('0x9')](isASupportedCipher);}function isAlgorithmSupported(_0x92ec89){return getSupportedCiphers()[_0xc276('0xa')](_0x92ec89)>-0x1;}function combineOptions(_0x42ce36){var _0x50feac={};for(var _0xedffda in fileOptions){if(fileOptions[_0xc276('0xb')](_0xedffda)){_0x50feac[_0xedffda]=fileOptions[_0xedffda];}}if(_0x42ce36){for(var _0x574bd3 in _0x42ce36){if(_0x42ce36[_0xc276('0xb')](_0x574bd3)){_0x50feac[_0x574bd3]=_0x42ce36[_0x574bd3];}}}return _0x50feac;}exports[_0xc276('0xc')]=function(_0x4f5ee0,_0x14a90c,_0x5c248e,_0x1600e5){return new BPromise(function(_0x311131,_0x236fdd){_0x1600e5=combineOptions(_0x1600e5);var _0x12d2b5=new Buffer(_0x5c248e);var _0xe99c7e=fs['createReadStream'](_0x4f5ee0);var _0xc1d8fd=fs[_0xc276('0xd')](_0x14a90c);var _0x16796a=crypto[_0xc276('0xe')](_0x1600e5['algorithm'],_0x12d2b5);_0xe99c7e['on']('data',function(_0x351520){var _0x26a2d3=new Buffer(_0x16796a['update'](_0x351520),_0xc276('0xf'));_0xc1d8fd[_0xc276('0x10')](_0x26a2d3);});_0xe99c7e['on'](_0xc276('0x11'),function(){try{var _0x199dff=new Buffer(_0x16796a[_0xc276('0x12')](_0xc276('0xf')),_0xc276('0xf'));_0xc1d8fd['write'](_0x199dff);_0xc1d8fd['end']();_0xc1d8fd['on'](_0xc276('0x13'),function(){return _0x311131(_0x14a90c);});}catch(_0x150d05){fs[_0xc276('0x14')](_0x14a90c);return _0x236fdd(_0x150d05);}});});};exports[_0xc276('0x15')]=function(_0x3d3eeb,_0x1bef59,_0x5e5114,_0x2ba16f){return new BPromise(function(_0xd8d4ad,_0x9e9afa){_0x2ba16f=combineOptions(_0x2ba16f);var _0x45c4a4=new Buffer(_0x5e5114);var _0x3ba3bc=fs[_0xc276('0x16')](_0x3d3eeb);var _0x547071=fs[_0xc276('0xd')](_0x1bef59);var _0x3aa312=crypto[_0xc276('0x17')](_0x2ba16f['algorithm'],_0x45c4a4);_0x3ba3bc['on'](_0xc276('0x18'),function(_0x1f156e){var _0x106aef=new Buffer(_0x3aa312[_0xc276('0x19')](_0x1f156e),'binary');_0x547071[_0xc276('0x10')](_0x106aef);});_0x3ba3bc['on'](_0xc276('0x11'),function(){try{var _0x394dbc=new Buffer(_0x3aa312[_0xc276('0x12')](_0xc276('0xf')),_0xc276('0xf'));_0x547071[_0xc276('0x10')](_0x394dbc);_0x547071[_0xc276('0x11')]();_0x547071['on'](_0xc276('0x13'),function(){return _0xd8d4ad(_0x1bef59);});}catch(_0x5412c8){fs['unlink'](_0x1bef59);return _0x9e9afa(_0x5412c8);}});});};exports[_0xc276('0x1a')]=function(_0xccae4a,_0x59b144,_0x2329d5){var _0x4afa4e=_0x2329d5||stringOptions['algorithm'];var _0x2215d1=_0x59b144||stringOptions[_0xc276('0x1b')];var _0x57470e=crypto['createCipher'](_0x4afa4e,_0x2215d1);var _0x11d5d0=_0x57470e['update'](_0xccae4a,_0xc276('0x1c'),_0xc276('0x1d'));_0x11d5d0+=_0x57470e[_0xc276('0x12')]('base64');return _0x11d5d0;};exports[_0xc276('0x1e')]=function(_0x2aea44,_0x5e86b7,_0x516ade){var _0x38c915=_0x516ade||stringOptions[_0xc276('0x7')];var _0xff2235=_0x5e86b7||stringOptions['secret'];var _0x15d502=crypto[_0xc276('0x17')](_0x38c915,_0xff2235);var _0x10f8a7=_0x15d502[_0xc276('0x19')](_0x2aea44,_0xc276('0x1d'),_0xc276('0x1c'));_0x10f8a7+=_0x15d502[_0xc276('0x12')](_0xc276('0x1c'));return _0x10f8a7;};