SEARCH-116 This is using CTR mode

This commit is contained in:
Keerthi Niranjan 2017-08-18 12:56:48 +05:30 committed by Keerthi Niranjan
parent f6ae42f854
commit eff466d199

View File

@ -11,41 +11,41 @@ const isDevEnv = require('../utils/misc.js').isDevEnv;
const userData = path.join(app.getPath('userData')); const userData = path.join(app.getPath('userData'));
const INDEX_DATA_FOLDER = isDevEnv ? './msgsjson' : path.join(userData, 'data'); const INDEX_DATA_FOLDER = isDevEnv ? './msgsjson' : path.join(userData, 'data');
const MODE = 'aes-256-ctr';
class Crypto { class Crypto {
constructor() { constructor() {
this.indexDataFolder = INDEX_DATA_FOLDER; this.indexDataFolder = INDEX_DATA_FOLDER;
this.decipher = crypto.createDecipher('aes256', 'temp'); this.key = '53796d70686f6e792074657374206b657920666f7220656e6372797074696f6e20';
this.cipher = crypto.createCipher('aes256', "temp");
this.dump = path.join(__dirname, '..', '..'); this.dump = path.join(__dirname, '..', '..');
this.encryptedIndex = 'encryptedIndex.enc'; this.encryptedIndex = 'encryptedIndex.enc';
this.zipErrored = false; this.zipErrored = false;
} }
encryption() { encryption() {
let self = this; return new Promise((resolve, reject) => {
return new Promise(function (resolve, reject) {
let output = fs.createWriteStream(`${self.dump}/content.zip`); let output = fs.createWriteStream(`${this.dump}/content.zip`);
output.on('close', function () { output.on('close', () => {
const input = fs.createReadStream(`${self.dump}/content.zip`); const input = fs.createReadStream(`${this.dump}/content.zip`);
const outPutEncryption = fs.createWriteStream(self.encryptedIndex); const outputEncryption = fs.createWriteStream(this.encryptedIndex);
const cipher = crypto.createCipher(MODE, this.key);
input.pipe(self.cipher).pipe(outPutEncryption).on('finish', function (err, res) { input.pipe(cipher).pipe(outputEncryption).on('finish', (err, res) => {
if (err) { if (err) {
reject(new Error(err)); reject(new Error(err));
} }
if (!self.zipErrored) { if (!this.zipErrored) {
fs.unlinkSync(`${self.dump}/content.zip`); fs.unlinkSync(`${this.dump}/content.zip`);
Crypto.deleteFolderRecursive(self.indexDataFolder) Crypto.deleteFolderRecursive(this.indexDataFolder)
.then(function () { .then(function () {
resolve(res); resolve(res);
}) })
.catch(function (error) { .catch(function (error) {
console.log(error) reject(new Error(error))
}); });
} }
}); });
@ -53,11 +53,11 @@ class Crypto {
zipArchive.pipe(output); zipArchive.pipe(output);
zipArchive.directory(self.indexDataFolder, true); zipArchive.directory(this.indexDataFolder, true);
zipArchive.finalize(function (err) { zipArchive.finalize((err) => {
if (err) { if (err) {
self.zipErrored = true; this.zipErrored = true;
reject(new Error(err)); reject(new Error(err));
} }
}); });
@ -65,41 +65,44 @@ class Crypto {
} }
decryption() { decryption() {
let self = this; return new Promise((resolve, reject) => {
return new Promise(function (resolve, reject) { const input = fs.createReadStream(this.encryptedIndex);
const output = fs.createWriteStream(`${this.dump}/decrypted.zip`);
const deCipher = crypto.createDecipher(MODE, this.key);
const input = fs.createReadStream(self.encryptedIndex); input.pipe(deCipher).pipe(output).on('finish', () => {
const output = fs.createWriteStream(`${self.dump}/decrypted.zip`); let readStream = fs.createReadStream(`${this.dump}/decrypted.zip`);
readStream
.on('data', (data) => {
if (!data) {
reject(new Error("error reading zip"));
}
unzip();
})
.on('error', (error) => {
reject(new Error(error.message));
});
});
function unzip() { let unzip = () => {
let temp = path.join(__dirname, '..', '..'); let temp = path.join(__dirname, '..', '..');
extract(`${self.dump}/decrypted.zip`, {dir: temp}, function (err) { extract(`${this.dump}/decrypted.zip`, {dir: temp}, (err) => {
if (err) reject(err); if (err) {
fs.unlink(`${self.dump}/decrypted.zip`, function () { reject(new Error(err));
}
fs.unlink(`${this.dump}/decrypted.zip`, () => {
resolve('success') resolve('success')
}); });
}) })
} };
input.pipe(self.decipher).pipe(output).on('finish', function () {
var readStream = fs.createReadStream(`${self.dump}/decrypted.zip`);
readStream
.on('data', function (data) {
if (!data) reject("error reading zip");
unzip();
})
.on('error', function (error) {
console.log('Error:', error.message);
});
});
}); });
} }
static deleteFolderRecursive(pt) { static deleteFolderRecursive(pt) {
return new Promise(function (resolve, reject) { return new Promise((resolve, reject) => {
if (fs.existsSync(pt)) { if (fs.existsSync(pt)) {
fs.readdirSync(pt).forEach(function (file) { fs.readdirSync(pt).forEach((file) => {
var curPath = pt + "/" + file; let curPath = pt + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { if (fs.lstatSync(curPath).isDirectory()) {
Crypto.deleteFolderRecursive(curPath); Crypto.deleteFolderRecursive(curPath);
} else { } else {