|
| 1 | +const AES = require('crypto-js/aes.js'); |
| 2 | +const CryptoJS = require('crypto-js/crypto-js.js'); |
| 3 | +const prompt = require('electron-prompt'); |
| 4 | +/** |
| 5 | + * A class that handles `encryption-*` web events in the renderer process |
| 6 | + * and performs data encryption/decryption. |
| 7 | + * |
| 8 | + * TODO: consider spawning another process for data encryption / decryption. |
| 9 | + * Compare gain/loss on running encryption/decryption in separate process |
| 10 | + * and check whether data passing from process to another process is more costly. |
| 11 | + */ |
| 12 | +class EncryptionService { |
| 13 | + constructor() { |
| 14 | + this._decodeHandler = this._decodeHandler.bind(this); |
| 15 | + this._encodeHandler = this._encodeHandler.bind(this); |
| 16 | + } |
| 17 | + |
| 18 | + listen() { |
| 19 | + window.addEventListener('encryption-decode', this._decodeHandler); |
| 20 | + window.addEventListener('encryption-encode', this._encodeHandler); |
| 21 | + } |
| 22 | + |
| 23 | + unlisten() { |
| 24 | + window.removeEventListener('encryption-decode', this._decodeHandler); |
| 25 | + window.removeEventListener('encryption-encode', this._encodeHandler); |
| 26 | + } |
| 27 | + |
| 28 | + _decodeHandler(e) { |
| 29 | + const { method } = e.detail; |
| 30 | + e.preventDefault(); |
| 31 | + e.detail.result = this.decode(method, e.detail); |
| 32 | + } |
| 33 | + |
| 34 | + _encodeHandler(e) { |
| 35 | + const { method } = e.detail; |
| 36 | + e.preventDefault(); |
| 37 | + e.detail.result = this.encode(method, e.detail); |
| 38 | + } |
| 39 | + |
| 40 | + async encode(method, opts) { |
| 41 | + switch (method) { |
| 42 | + case 'aes': return await this.encodeAes(opts.data, opts.passphrase); |
| 43 | + default: throw new Error(`Unknown encryption method`); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + async encodeAes(data, passphrase) { |
| 48 | + // Todo: this looks really dangerous to run file encryption in the main |
| 49 | + // thread (of the renderer process). Consider other options. |
| 50 | + const encrypted = AES.encrypt(data, passphrase); |
| 51 | + return encrypted.toString(); |
| 52 | + } |
| 53 | + |
| 54 | + async decode(method, opts) { |
| 55 | + switch (method) { |
| 56 | + case 'aes': return await this.decodeAes(opts.data, opts.passphrase); |
| 57 | + default: throw new Error(`Unknown decryption method`); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + async decodeAes(data, passphrase) { |
| 62 | + if (passphrase === undefined) { |
| 63 | + const win = require('electron').remote.getCurrentWindow(); |
| 64 | + passphrase = await prompt({ |
| 65 | + title: 'File password', |
| 66 | + label: 'Enter password to open the file', |
| 67 | + }, win); |
| 68 | + if (passphrase === null) { |
| 69 | + throw new Error('Password is required to open the file.'); |
| 70 | + } |
| 71 | + } |
| 72 | + try { |
| 73 | + const bytes = AES.decrypt(data, passphrase); |
| 74 | + return bytes.toString(CryptoJS.enc.Utf8); |
| 75 | + } catch (_) { |
| 76 | + throw new Error('Invalid password.'); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +exports.EncryptionService = EncryptionService; |
0 commit comments