31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
// Ersatz für die von store.js genutzten Electron-APIs unter Android.
|
|
// safeStorage verschlüsselt mit einem 256-Bit-Schlüssel, den die App im Android Keystore verpackt aufbewahrt
|
|
// und beim Start als MRTERM_VAULT_KEY (base64) an den Node-Prozess übergibt.
|
|
const crypto = require('crypto');
|
|
|
|
const dataDir = () => process.env.DATADIR || require('bridge').getDataPath();
|
|
const key = () => {
|
|
const k = Buffer.from(process.env.MRTERM_VAULT_KEY || '', 'base64');
|
|
return k.length === 32 ? k : null;
|
|
};
|
|
|
|
const safeStorage = {
|
|
isEncryptionAvailable: () => !!key(),
|
|
encryptString(plain) {
|
|
const iv = crypto.randomBytes(12);
|
|
const c = crypto.createCipheriv('aes-256-gcm', key(), iv);
|
|
const ct = Buffer.concat([c.update(String(plain), 'utf8'), c.final()]);
|
|
return Buffer.concat([iv, c.getAuthTag(), ct]);
|
|
},
|
|
decryptString(buf) {
|
|
const d = crypto.createDecipheriv('aes-256-gcm', key(), buf.subarray(0, 12));
|
|
d.setAuthTag(buf.subarray(12, 28));
|
|
return Buffer.concat([d.update(buf.subarray(28)), d.final()]).toString('utf8');
|
|
},
|
|
};
|
|
|
|
module.exports = {
|
|
app: { getPath: () => dataDir(), getVersion: () => process.env.MRTERM_VERSION || '0.0.0', getLocale: () => process.env.MRTERM_LOCALE || 'en' },
|
|
safeStorage,
|
|
};
|