Built motion from commit bab6f49e.|2.6.17
[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 _0x9b22=['filter','indexOf','hasOwnProperty','createReadStream','createCipher','data','update','write','end','final','binary','decryptFile','createWriteStream','unlink','encryptString','utf8','base64','decryptString','secret','createDecipher','crypto','bluebird','app','session','aes-256-ctr','encrypt','algorithm','join','error','is\x20not\x20a\x20supported\x20algorithm.\x20Your\x20system\x20supports\x20the\x20following\x20algorithms:','getCiphers'];(function(_0x2d488d,_0x2f1a83){var _0x254418=function(_0x1b739a){while(--_0x1b739a){_0x2d488d['push'](_0x2d488d['shift']());}};_0x254418(++_0x2f1a83);}(_0x9b22,0x1a7));var _0x29b2=function(_0x475f26,_0x3b5938){_0x475f26=_0x475f26-0x0;var _0x56acba=_0x9b22[_0x475f26];return _0x56acba;};'use strict';var crypto=require(_0x29b2('0x0'));var fs=require('fs');var BPromise=require(_0x29b2('0x1'));var _=require('lodash');var config=require('../../config/environment');var logger=require('../../config/logger')(_0x29b2('0x2'));const AES_SUPPORTED_RX=/^aes(-)?\d{3}(-(cbc|cfb|ctr)(1|8)?){0,1}$/;const stringOptions={'secret':config['secrets'][_0x29b2('0x3')],'algorithm':_0x29b2('0x4')};const fileOptions={'algorithm':'aes192'};if(config[_0x29b2('0x5')]&&config['encrypt'][_0x29b2('0x6')]){var isSupported=isAlgorithmSupported(config[_0x29b2('0x5')][_0x29b2('0x6')]);if(isSupported){fileOptions[_0x29b2('0x6')]=config[_0x29b2('0x5')][_0x29b2('0x6')];}else{var supportedAlgorithms=getSupportedCiphers()[_0x29b2('0x7')](',\x20');logger[_0x29b2('0x8')](config[_0x29b2('0x5')][_0x29b2('0x6')],_0x29b2('0x9'),supportedAlgorithms);}}function isASupportedCipher(_0x128a16){return AES_SUPPORTED_RX['test'](_0x128a16);}function getSupportedCiphers(){return crypto[_0x29b2('0xa')]()[_0x29b2('0xb')](isASupportedCipher);}function isAlgorithmSupported(_0x18c1e2){return getSupportedCiphers()[_0x29b2('0xc')](_0x18c1e2)>-0x1;}function combineOptions(_0x44cace){var _0x2a81bf={};for(var _0xccf1f2 in fileOptions){if(fileOptions[_0x29b2('0xd')](_0xccf1f2)){_0x2a81bf[_0xccf1f2]=fileOptions[_0xccf1f2];}}if(_0x44cace){for(var _0x17600d in _0x44cace){if(_0x44cace[_0x29b2('0xd')](_0x17600d)){_0x2a81bf[_0x17600d]=_0x44cace[_0x17600d];}}}return _0x2a81bf;}exports['encryptFile']=function(_0x3555a5,_0x78f32e,_0x16e991,_0x30244f){return new BPromise(function(_0x54de65,_0x3fc4ac){_0x30244f=combineOptions(_0x30244f);var _0x275b05=new Buffer(_0x16e991);var _0xce8bbe=fs[_0x29b2('0xe')](_0x3555a5);var _0x58c15b=fs['createWriteStream'](_0x78f32e);var _0x447e38=crypto[_0x29b2('0xf')](_0x30244f['algorithm'],_0x275b05);_0xce8bbe['on'](_0x29b2('0x10'),function(_0x4abcd3){var _0xb79bdd=new Buffer(_0x447e38[_0x29b2('0x11')](_0x4abcd3),'binary');_0x58c15b[_0x29b2('0x12')](_0xb79bdd);});_0xce8bbe['on'](_0x29b2('0x13'),function(){try{var _0x4b8b99=new Buffer(_0x447e38[_0x29b2('0x14')](_0x29b2('0x15')),_0x29b2('0x15'));_0x58c15b[_0x29b2('0x12')](_0x4b8b99);_0x58c15b[_0x29b2('0x13')]();_0x58c15b['on']('close',function(){return _0x54de65(_0x78f32e);});}catch(_0xb114ca){fs['unlink'](_0x78f32e);return _0x3fc4ac(_0xb114ca);}});});};exports[_0x29b2('0x16')]=function(_0x42e3c6,_0x2929bb,_0x3aad23,_0x5812b2){return new BPromise(function(_0x1ce916,_0x1bdeb7){_0x5812b2=combineOptions(_0x5812b2);var _0x2bd9d7=new Buffer(_0x3aad23);var _0x551441=fs[_0x29b2('0xe')](_0x42e3c6);var _0x362436=fs[_0x29b2('0x17')](_0x2929bb);var _0x1a27c8=crypto['createDecipher'](_0x5812b2[_0x29b2('0x6')],_0x2bd9d7);_0x551441['on'](_0x29b2('0x10'),function(_0x4cc889){var _0x4cd9ea=new Buffer(_0x1a27c8[_0x29b2('0x11')](_0x4cc889),'binary');_0x362436[_0x29b2('0x12')](_0x4cd9ea);});_0x551441['on'](_0x29b2('0x13'),function(){try{var _0x833616=new Buffer(_0x1a27c8[_0x29b2('0x14')]('binary'),'binary');_0x362436['write'](_0x833616);_0x362436[_0x29b2('0x13')]();_0x362436['on']('close',function(){return _0x1ce916(_0x2929bb);});}catch(_0x316757){fs[_0x29b2('0x18')](_0x2929bb);return _0x1bdeb7(_0x316757);}});});};exports[_0x29b2('0x19')]=function(_0x330aa4,_0x2baa62,_0x4488be){var _0x4baa2d=_0x4488be||stringOptions[_0x29b2('0x6')];var _0x2d6ad0=_0x2baa62||stringOptions['secret'];var _0x1cd7e2=crypto[_0x29b2('0xf')](_0x4baa2d,_0x2d6ad0);var _0x1a8324=_0x1cd7e2[_0x29b2('0x11')](_0x330aa4,_0x29b2('0x1a'),_0x29b2('0x1b'));_0x1a8324+=_0x1cd7e2['final'](_0x29b2('0x1b'));return _0x1a8324;};exports[_0x29b2('0x1c')]=function(_0x3e9df5,_0x25d4f0,_0x229f7c){var _0x1c2a38=_0x229f7c||stringOptions[_0x29b2('0x6')];var _0x491c73=_0x25d4f0||stringOptions[_0x29b2('0x1d')];var _0x3926d2=crypto[_0x29b2('0x1e')](_0x1c2a38,_0x491c73);var _0x5bc9bd=_0x3926d2[_0x29b2('0x11')](_0x3e9df5,_0x29b2('0x1b'),_0x29b2('0x1a'));_0x5bc9bd+=_0x3926d2[_0x29b2('0x14')](_0x29b2('0x1a'));return _0x5bc9bd;};