Skip to content

Commit 4fe96b9

Browse files
committed
chore: started working towards fixing #194 - adding encryption options
1 parent 2df644d commit 4fe96b9

File tree

5 files changed

+77
-95
lines changed

5 files changed

+77
-95
lines changed

app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ArcInit {
2222
/* global ipc, ArcContextMenu, ArcElectronDrive, OAuth2Handler,
2323
ThemeManager, ArcPreferencesProxy, CookieBridge, WorkspaceManager,
2424
FilesystemProxy, ElectronAmfService, versionInfo, WindowSearchService,
25-
UpgradeHelper, ImportFilePrePprocessor */
25+
UpgradeHelper, ImportFilePrePprocessor, EncryptionService */
2626
this.created = false;
2727
this.contextActions = new ArcContextMenu();
2828
this.driveBridge = new ArcElectronDrive();
@@ -33,6 +33,7 @@ class ArcInit {
3333
this.fs = new FilesystemProxy();
3434
this.amfService = new ElectronAmfService();
3535
this.search = new WindowSearchService();
36+
this.encryption = new EncryptionService();
3637
}
3738
/**
3839
* @return {ImportFilePrePprocessor} Instance of import processor class.
@@ -66,6 +67,7 @@ class ArcInit {
6667
this.fs.listen();
6768
this.amfService.listen();
6869
this.search.listen();
70+
this.encryption.listen();
6971

7072
ipc.on('checking-for-update', () => {
7173
this.updateEventHandler('checking-for-update');

package-lock.json

Lines changed: 5 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"amf-client-js": "^3.5.1",
7979
"camelcase": "^4.1.0",
8080
"codemirror": "^5.49.0",
81+
"crypto-js": "^3.1.9-1",
8182
"electron-log": "^3.0.8",
8283
"electron-updater": "4.1.2",
8384
"form-data": "^2.5.1",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const AES = require('crypto-js/aes.js');
2+
const CryptoJS = require('crypto-js/crypto-js.js');
3+
/**
4+
* A class that handles `encryption-*` web events in the renderer process
5+
* and performs data encryption/decryption.
6+
*
7+
* TODO: consider spawning another process for data encryption / decryption.
8+
* Compare gain/loss on running encryption/decryption in separate process
9+
* and check whether data passing from process to another process is more costly.
10+
*/
11+
class EncryptionService {
12+
constructor() {
13+
this._decodeHandler = this._decodeHandler.bind(this);
14+
this._encodeHandler = this._encodeHandler.bind(this);
15+
}
16+
17+
listen() {
18+
window.addEventListener('encryption-decode', this._decodeHandler);
19+
window.addEventListener('encryption-encode', this._encodeHandler);
20+
}
21+
22+
unlisten() {
23+
window.removeEventListener('encryption-decode', this._decodeHandler);
24+
window.removeEventListener('encryption-encode', this._encodeHandler);
25+
}
26+
27+
_decodeHandler(e) {
28+
const { method } = e.detail;
29+
e.detail.result = this.decode(method, e.detail);
30+
}
31+
32+
_encodeHandler(e) {
33+
const { method } = e.detail;
34+
e.detail.result = this.encode(method, e.detail);
35+
}
36+
37+
async encode(method, opts) {
38+
switch (method) {
39+
case 'aes': return await this.encodeAes(opts.data, opts.passphrase);
40+
default: throw new Error(`Unknown encryption method`);
41+
}
42+
}
43+
44+
async encodeAes(data, passphrase) {
45+
// Todo: this looks really dangerous to run file encryption in the main
46+
// thread (of the renderer process). Consider other options.
47+
const encrypted = AES.encrypt(data, passphrase);
48+
return encrypted.toString();
49+
}
50+
51+
async decode(method, opts) {
52+
switch (method) {
53+
case 'aes': return await this.decodeAes(opts.data, opts.passphrase);
54+
default: throw new Error(`Unknown decryption method`);
55+
}
56+
}
57+
58+
async decodeAes(data, passphrase) {
59+
if (!passphrase === undefined) {
60+
passphrase = prompt('Enter password to open the file.');
61+
}
62+
const bytes = AES.decrypt(data, passphrase);
63+
return bytes.toString(CryptoJS.enc.Utf8);
64+
}
65+
}
66+
exports.EncryptionService = EncryptionService;

scripts/renderer/preload.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const { ArcContextMenu } = require('../packages/context-actions/renderer');
1414
const { FilesystemProxy } = require('./filesystem-proxy');
1515
const { ElectronAmfService } = require('@advanced-rest-client/electron-amf-service');
1616
const { WindowSearchService } = require('../packages/search-service/renderer');
17+
const { EncryptionService } = require('../packages/encryption/renderer/encryption.js');
1718
const { UpgradeHelper } = require('./upgrade-helper');
1819
const { ImportFilePrePprocessor } = require('./import-file-preprocessor');
1920
const setImmediateFn = setImmediate;
@@ -49,6 +50,7 @@ process.once('loaded', () => {
4950
global.Jexl = Jexl;
5051
global.UpgradeHelper = UpgradeHelper;
5152
global.ImportFilePrePprocessor = ImportFilePrePprocessor;
53+
global.EncryptionService = EncryptionService;
5254
global.versionInfo = {
5355
chrome: versions.chrome,
5456
appVersion: app.getVersion()

0 commit comments

Comments
 (0)