2017-11-14 21:54:12 -05:00
|
|
|
"use strict";
|
|
|
|
|
2020-09-25 20:55:45 +02:00
|
|
|
const log = require('./log');
|
2018-04-01 21:27:46 -04:00
|
|
|
const dataEncryptionService = require('./data_encryption');
|
2017-11-09 23:25:23 -05:00
|
|
|
|
2021-05-07 22:23:49 +02:00
|
|
|
let dataKey = null;
|
2018-04-01 21:27:46 -04:00
|
|
|
|
2018-04-20 00:12:01 -04:00
|
|
|
function setDataKey(decryptedDataKey) {
|
2021-05-07 22:23:49 +02:00
|
|
|
dataKey = Array.from(decryptedDataKey);
|
2018-03-31 08:53:52 -04:00
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function getDataKey() {
|
2021-05-07 22:23:49 +02:00
|
|
|
return dataKey;
|
2018-02-23 22:58:24 -05:00
|
|
|
}
|
|
|
|
|
2021-04-03 22:02:25 +02:00
|
|
|
function resetDataKey() {
|
2021-05-07 22:23:49 +02:00
|
|
|
dataKey = null;
|
2021-04-03 22:02:25 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 00:12:01 -04:00
|
|
|
function isProtectedSessionAvailable() {
|
2021-05-07 22:23:49 +02:00
|
|
|
return !!dataKey;
|
2017-11-14 21:54:12 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 08:53:52 -04:00
|
|
|
function decryptNotes(notes) {
|
2020-09-25 20:55:45 +02:00
|
|
|
try {
|
|
|
|
for (const note of notes) {
|
|
|
|
if (note.isProtected) {
|
|
|
|
note.title = decryptString(note.title);
|
|
|
|
}
|
2019-03-30 19:24:53 +01:00
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
}
|
2020-09-25 20:55:45 +02:00
|
|
|
catch (e) {
|
|
|
|
log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`);
|
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
}
|
|
|
|
|
2019-10-31 21:58:34 +01:00
|
|
|
function encrypt(plainText) {
|
2020-12-21 22:08:55 +01:00
|
|
|
if (plainText === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:58:34 +01:00
|
|
|
return dataEncryptionService.encrypt(getDataKey(), plainText);
|
2019-02-06 20:19:25 +01:00
|
|
|
}
|
2018-01-24 22:13:41 -05:00
|
|
|
|
2019-10-31 21:58:34 +01:00
|
|
|
function decrypt(cipherText) {
|
2020-12-21 22:08:55 +01:00
|
|
|
if (cipherText === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:05:33 +01:00
|
|
|
return dataEncryptionService.decrypt(getDataKey(), cipherText);
|
2018-01-24 22:13:41 -05:00
|
|
|
}
|
|
|
|
|
2019-11-02 07:50:23 +01:00
|
|
|
function decryptString(cipherText) {
|
|
|
|
return dataEncryptionService.decryptString(getDataKey(), cipherText);
|
|
|
|
}
|
|
|
|
|
2017-11-09 23:25:23 -05:00
|
|
|
module.exports = {
|
|
|
|
setDataKey,
|
2021-04-03 22:02:25 +02:00
|
|
|
resetDataKey,
|
2018-01-24 22:13:41 -05:00
|
|
|
isProtectedSessionAvailable,
|
2019-10-31 21:58:34 +01:00
|
|
|
encrypt,
|
|
|
|
decrypt,
|
2019-11-02 07:50:23 +01:00
|
|
|
decryptString,
|
2021-05-07 22:23:49 +02:00
|
|
|
decryptNotes
|
2020-06-15 17:56:53 +02:00
|
|
|
};
|