SymphonyElectron/js/cryptoLib/index.js

120 lines
4.7 KiB
JavaScript
Raw Normal View History

'use strict';
const path = require('path');
2017-08-14 00:17:01 -05:00
const fs = require('fs');
const lz4 = require('../compressionLib');
const isDevEnv = require('../utils/misc.js').isDevEnv;
2017-08-18 04:18:22 -05:00
const crypto = require('./crypto');
const log = require('../log.js');
const logLevels = require('../enums/logLevels.js');
const searchConfig = require('../search/searchConfig.js');
const DUMP_PATH = isDevEnv ? path.join(__dirname, '..', '..') : searchConfig.FOLDERS_CONSTANTS.USER_DATA_PATH;
class Crypto {
constructor(userId, key) {
2017-11-28 04:44:13 -06:00
this.indexDataFolder = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME_PATH}_${userId}_${searchConfig.INDEX_VERSION}`;
this.permanentIndexName = `${searchConfig.FOLDERS_CONSTANTS.PREFIX_NAME}_${userId}_${searchConfig.INDEX_VERSION}`;
this.dump = DUMP_PATH;
this.key = key;
this.encryptedIndex = `${DUMP_PATH}/${this.permanentIndexName}.enc`;
this.dataFolder = searchConfig.FOLDERS_CONSTANTS.INDEX_PATH;
}
2017-08-18 04:18:22 -05:00
/**
* Compressing the user index folder and
* encrypting it
2017-08-18 04:18:22 -05:00
* @returns {Promise}
*/
encryption() {
2017-08-18 02:26:48 -05:00
return new Promise((resolve, reject) => {
2017-08-14 00:17:01 -05:00
if (!fs.existsSync(this.indexDataFolder)){
2017-11-28 04:44:13 -06:00
log.send(logLevels.ERROR, 'Crypto: User index folder not found');
reject();
return;
}
lz4.compression(`${searchConfig.FOLDERS_CONSTANTS.INDEX_FOLDER_NAME}/${this.permanentIndexName}`,
`${this.permanentIndexName}`, (error, response) => {
if (error) {
2017-11-28 04:44:13 -06:00
log.send(logLevels.ERROR, 'Crypto: Error while compressing to lz4: ' + error);
reject(error);
return;
2017-08-14 00:17:01 -05:00
}
if (response && response.stderr) {
2017-11-28 04:44:13 -06:00
log.send(logLevels.WARN, 'Crypto: Child process stderr while compression, ' + response.stderr);
}
const input = fs.createReadStream(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`);
const outputEncryption = fs.createWriteStream(this.encryptedIndex);
let config = {
key: this.key
};
const encrypt = crypto.encrypt(config);
let encryptionProcess = input.pipe(encrypt).pipe(outputEncryption);
encryptionProcess.on('finish', (err) => {
if (err) {
2017-11-28 04:44:13 -06:00
log.send(logLevels.ERROR, 'Crypto: Error while encrypting the compressed file: ' + err);
reject(new Error(err));
return;
}
fs.unlinkSync(`${this.dump}/${this.permanentIndexName}${searchConfig.TAR_LZ4_EXT}`);
resolve('Success');
});
2017-08-14 00:17:01 -05:00
});
});
}
2017-08-18 04:18:22 -05:00
/**
* Decrypting the .enc file and unzipping
* removing the .enc file and the dump files
* @returns {Promise}
*/
decryption() {
2017-08-18 02:26:48 -05:00
return new Promise((resolve, reject) => {
if (!fs.existsSync(this.encryptedIndex)){
2017-11-28 04:44:13 -06:00
log.send(logLevels.ERROR, 'Crypto: Encrypted file not found');
reject();
return;
}
2017-08-18 02:26:48 -05:00
const input = fs.createReadStream(this.encryptedIndex);
const output = fs.createWriteStream(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`);
2017-08-18 04:18:22 -05:00
let config = {
key: this.key
2017-08-18 04:18:22 -05:00
};
const decrypt = crypto.decrypt(config);
2017-08-14 00:17:01 -05:00
2017-10-18 00:46:56 -05:00
let decryptionProcess = input.pipe(decrypt).pipe(output);
decryptionProcess.on('finish', () => {
if (!fs.existsSync(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`)){
log.send(logLevels.ERROR, 'decrypted.tar.lz4 file not found');
reject();
return;
}
lz4.deCompression(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`,(error, response) => {
if (error) {
2017-11-28 04:44:13 -06:00
log.send(logLevels.ERROR, 'Crypto: Error while deCompression, ' + error);
// no return, need to unlink if error
}
2017-11-02 06:13:35 -05:00
if (response && response.stderr) {
2017-11-28 04:44:13 -06:00
log.send(logLevels.WARN, 'Crypto: Child process stderr while deCompression, ' + response.stderr);
2017-11-02 06:13:35 -05:00
}
fs.unlink(`${this.dump}/decrypted${searchConfig.TAR_LZ4_EXT}`, () => {
resolve('success');
2017-08-18 02:26:48 -05:00
});
})
});
2017-08-14 00:17:01 -05:00
});
}
}
module.exports = Crypto;