Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypt and compress json file #945

Open
wants to merge 3 commits into
base: sync
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions __tests__/__main__/encrypt-compress-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
'use strict';

const {
compressEncryptFile,
decryptDecompressFile
} = require('../../js/encrypt-compress-json');

const path = require('path');
const fs = require('fs');


describe('Encrypt compress JSON', function()
{
process.env.NODE_ENV = 'test';

const entriesContent =
`[{"type": "flexible","date": "2020-4-1","values": ["08:00","12:00","13:00","17:00"]},
{"type": "flexible","date": "2020-4-2","values": ["07:00","11:00","14:00","18:00"]},
{"type": "waived","date": "2019-12-31","data": "New Year's eve","hours": "08:00"},
{"type": "waived","date": "2020-01-01","data": "New Year's Day","hours": "08:00"},
{"type": "waived","date": "2020-04-10","data": "Good Friday","hours": "08:00"}]`;

const folder = fs.mkdtempSync('encrypt-decrypt');
const filePath = path.join('.', folder, 'regular.ttldb');
const filePathCompressed = path.join('.',folder, 'compressed.tgz');
const filePathDecompressed = path.join('.',folder, 'decompressed');

const filePathCompressedFail = path.join('.',folder, 'compressed-fail.tgz');

const password = 'Password_123';
const wrongPassword = 'NOTpassword_123';
const invalidPassword = '123';

fs.writeFileSync(`${folder}/regular.ttldb`, entriesContent, 'utf8');

describe('de/compress and de/encrypt file', function()
{
test('should compress and encrypt successfully', async() =>
{
const compressSuccess = await compressEncryptFile(filePath, filePathCompressed, password);
expect(compressSuccess).toBeTruthy();
});

test('should NOT compress and encrypt successfully', async() =>
{
// invalid password, see cryptify documentation for password rules
const compressFail = await compressEncryptFile(filePath, filePathCompressedFail, invalidPassword);
expect(compressFail).not.toBeTruthy();
// invalid path
const compressFail2 = await compressEncryptFile('invalid/path', filePathCompressedFail, password);
expect(compressFail2).not.toBeTruthy();
});

test('should decompress successfully', async() =>
{
const decompressSuccess = await decryptDecompressFile(filePathCompressed, filePathDecompressed, password);
expect(decompressSuccess).toBeTruthy();
});

test('should NOT decompress successfully', async() =>
{
// wrong password
const decompressFail = await decryptDecompressFile(filePathCompressed, filePathDecompressed, wrongPassword);
expect(decompressFail).not.toBeTruthy();
// invalid path
const decompressFail2 = await decryptDecompressFile('invalid/path', filePathDecompressed, password);
expect(decompressFail2).not.toBeTruthy();
});

test('decompressed file should equal file before', async() =>
{
expect(fs.readFileSync(filePath)).toEqual(fs.readFileSync(path.join(filePathDecompressed, 'regular.ttldb')));
});

test('compressed file should NOT equal file before', async() =>
{
expect(fs.readFileSync(filePath)).not.toEqual(fs.readFileSync(filePathCompressed));
});
});

afterAll(() =>
{
fs.rmdirSync(folder, {recursive: true});
});

});
40 changes: 40 additions & 0 deletions js/encrypt-compress-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
import Cryptify from 'cryptify';
const compressing = require('compressing');

async function compressEncryptFile(filePathOriginal, filePathCompressed, passPhrase)
{
try
{
await compressing.tgz.compressFile(filePathOriginal, filePathCompressed);
const instance = new Cryptify(filePathCompressed, passPhrase, 'aes-256-cbc', 'utf8', true);
await instance.encrypt();
return true;
}
catch (e)
{
console.log(`compression and encryption failed: ${e}`);
return false;
}
}

async function decryptDecompressFile(filePathCompressed, filePathDecompressed, passPhrase)
{
try
{
const instance = new Cryptify(filePathCompressed, passPhrase, 'aes-256-cbc', 'utf8', true);
await instance.decrypt();
await compressing.tgz.uncompress(filePathCompressed, filePathDecompressed);
return true;
}
catch (e)
{
console.log(`decryption and decompression failed: ${e}`);
return false;
}
}

module.exports = {
compressEncryptFile,
decryptDecompressFile
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"dependencies": {
"@fortawesome/fontawesome-free": "^5.15.4",
"bootstrap": "^5.1.1",
"compressing": "^1.7.0",
"cryptify": "^4.1.2",
"date-holidays": "^3.9.1",
"electron-squirrel-startup": "^1.0.0",
"electron-store": "^6.0.1",
Expand Down