Built motion from commit f67b3b02.|2.6.31
[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 _0x7a2b=['unlink','secret','utf8','base64','final','createDecipher','crypto','bluebird','lodash','../../config/logger','app','session','aes-256-ctr','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','test','indexOf','encryptFile','createReadStream','createWriteStream','createCipher','data','update','binary','write','end','close'];(function(_0x356184,_0x42ee78){var _0x36d3b6=function(_0x1b4004){while(--_0x1b4004){_0x356184['push'](_0x356184['shift']());}};_0x36d3b6(++_0x42ee78);}(_0x7a2b,0x1c8));var _0xb7a2=function(_0x618414,_0x1a7a60){_0x618414=_0x618414-0x0;var _0x21c19a=_0x7a2b[_0x618414];return _0x21c19a;};'use strict';var crypto=require(_0xb7a2('0x0'));var fs=require('fs');var BPromise=require(_0xb7a2('0x1'));var _=require(_0xb7a2('0x2'));var config=require('../../config/environment');var logger=require(_0xb7a2('0x3'))(_0xb7a2('0x4'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config['secrets'][_0xb7a2('0x5')],'algorithm':_0xb7a2('0x6')};const fileOptions={'algorithm':'aes192'};if(config[_0xb7a2('0x7')]&&config[_0xb7a2('0x7')][_0xb7a2('0x8')]){var isSupported=isAlgorithmSupported(config[_0xb7a2('0x7')][_0xb7a2('0x8')]);if(isSupported){fileOptions[_0xb7a2('0x8')]=config[_0xb7a2('0x7')]['algorithm'];}else{var supportedAlgorithms=getSupportedCiphers()[_0xb7a2('0x9')](',\x20');logger[_0xb7a2('0xa')](config[_0xb7a2('0x7')]['algorithm'],_0xb7a2('0xb'),supportedAlgorithms);}}function isASupportedCipher(_0x2c79eb){return AES_SUPPORTED_RX[_0xb7a2('0xc')](_0x2c79eb);}function getSupportedCiphers(){return crypto['getCiphers']()['filter'](isASupportedCipher);}function isAlgorithmSupported(_0x4f11c9){return getSupportedCiphers()[_0xb7a2('0xd')](_0x4f11c9)>-0x1;}function combineOptions(_0x4f0ec4){var _0x250349={};for(var _0x39ac1f in fileOptions){if(fileOptions['hasOwnProperty'](_0x39ac1f)){_0x250349[_0x39ac1f]=fileOptions[_0x39ac1f];}}if(_0x4f0ec4){for(var _0x59a299 in _0x4f0ec4){if(_0x4f0ec4['hasOwnProperty'](_0x59a299)){_0x250349[_0x59a299]=_0x4f0ec4[_0x59a299];}}}return _0x250349;}exports[_0xb7a2('0xe')]=function(_0x21eb67,_0x6f8de0,_0x2c5e2a,_0x5c2618){return new BPromise(function(_0x3942ea,_0x1ccf44){_0x5c2618=combineOptions(_0x5c2618);var _0xf848a7=new Buffer(_0x2c5e2a);var _0x543467=fs[_0xb7a2('0xf')](_0x21eb67);var _0x335dfc=fs[_0xb7a2('0x10')](_0x6f8de0);var _0x333120=crypto[_0xb7a2('0x11')](_0x5c2618[_0xb7a2('0x8')],_0xf848a7);_0x543467['on'](_0xb7a2('0x12'),function(_0x42872c){var _0x37d8a6=new Buffer(_0x333120[_0xb7a2('0x13')](_0x42872c),_0xb7a2('0x14'));_0x335dfc[_0xb7a2('0x15')](_0x37d8a6);});_0x543467['on'](_0xb7a2('0x16'),function(){try{var _0x447781=new Buffer(_0x333120['final'](_0xb7a2('0x14')),'binary');_0x335dfc['write'](_0x447781);_0x335dfc[_0xb7a2('0x16')]();_0x335dfc['on'](_0xb7a2('0x17'),function(){return _0x3942ea(_0x6f8de0);});}catch(_0x508c2e){fs[_0xb7a2('0x18')](_0x6f8de0);return _0x1ccf44(_0x508c2e);}});});};exports['decryptFile']=function(_0x49eaa7,_0x4cbe3c,_0x5d8dac,_0x3f3c10){return new BPromise(function(_0x1906d7,_0x18673e){_0x3f3c10=combineOptions(_0x3f3c10);var _0x1047e7=new Buffer(_0x5d8dac);var _0x5bc6df=fs['createReadStream'](_0x49eaa7);var _0x1577a7=fs['createWriteStream'](_0x4cbe3c);var _0x1c9859=crypto['createDecipher'](_0x3f3c10[_0xb7a2('0x8')],_0x1047e7);_0x5bc6df['on'](_0xb7a2('0x12'),function(_0x4f1701){var _0x34f212=new Buffer(_0x1c9859[_0xb7a2('0x13')](_0x4f1701),'binary');_0x1577a7[_0xb7a2('0x15')](_0x34f212);});_0x5bc6df['on']('end',function(){try{var _0x87bdc=new Buffer(_0x1c9859['final'](_0xb7a2('0x14')),'binary');_0x1577a7[_0xb7a2('0x15')](_0x87bdc);_0x1577a7[_0xb7a2('0x16')]();_0x1577a7['on'](_0xb7a2('0x17'),function(){return _0x1906d7(_0x4cbe3c);});}catch(_0x542012){fs[_0xb7a2('0x18')](_0x4cbe3c);return _0x18673e(_0x542012);}});});};exports['encryptString']=function(_0x360010,_0x180f2d,_0x54ec29){var _0x259141=_0x54ec29||stringOptions[_0xb7a2('0x8')];var _0x27ffef=_0x180f2d||stringOptions[_0xb7a2('0x19')];var _0x434e3b=crypto[_0xb7a2('0x11')](_0x259141,_0x27ffef);var _0x50ccf6=_0x434e3b[_0xb7a2('0x13')](_0x360010,_0xb7a2('0x1a'),_0xb7a2('0x1b'));_0x50ccf6+=_0x434e3b[_0xb7a2('0x1c')](_0xb7a2('0x1b'));return _0x50ccf6;};exports['decryptString']=function(_0x43aecb,_0x8cc3fa,_0x32c78c){var _0x3164ce=_0x32c78c||stringOptions[_0xb7a2('0x8')];var _0x2e364f=_0x8cc3fa||stringOptions[_0xb7a2('0x19')];var _0x208f3b=crypto[_0xb7a2('0x1d')](_0x3164ce,_0x2e364f);var _0x4d34df=_0x208f3b[_0xb7a2('0x13')](_0x43aecb,_0xb7a2('0x1b'),_0xb7a2('0x1a'));_0x4d34df+=_0x208f3b[_0xb7a2('0x1c')]('utf8');return _0x4d34df;};