2017-11-16 11:13:45 +08:00
|
|
|
const sql = require('../services/sql');
|
|
|
|
const data_encryption = require('../services/data_encryption');
|
|
|
|
const password_encryption = require('../services/password_encryption');
|
|
|
|
const readline = require('readline');
|
|
|
|
|
|
|
|
const cl = readline.createInterface(process.stdin, process.stdout);
|
|
|
|
|
|
|
|
function question(q) {
|
|
|
|
return new Promise( (res, rej) => {
|
|
|
|
cl.question( q, answer => {
|
|
|
|
res(answer);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = async () => {
|
|
|
|
const password = await question("Enter password: ");
|
|
|
|
const dataKey = await password_encryption.getDecryptedDataKey(password);
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
const protectedNotes = await sql.getRows("SELECT * FROM notes WHERE is_protected = 1");
|
2017-11-16 11:13:45 +08:00
|
|
|
|
|
|
|
for (const note of protectedNotes) {
|
|
|
|
const decryptedTitle = data_encryption.decrypt(dataKey, note.note_title);
|
|
|
|
|
2017-11-19 01:53:17 +08:00
|
|
|
note.note_title = data_encryption.encrypt(dataKey, "0" + note.note_id, decryptedTitle);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
|
|
|
const decryptedText = data_encryption.decrypt(dataKey, note.note_text);
|
2017-11-19 01:53:17 +08:00
|
|
|
note.note_text = data_encryption.encrypt(dataKey, "1" + note.note_id, decryptedText);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
|
|
|
await sql.execute("UPDATE notes SET note_title = ?, note_text = ? WHERE note_id = ?", [note.note_title, note.note_text, note.note_id]);
|
|
|
|
}
|
|
|
|
|
2018-01-30 06:41:59 +08:00
|
|
|
const protectedNotesHistory = await sql.getRows("SELECT * FROM notes_history WHERE is_protected = 1");
|
2017-11-16 11:13:45 +08:00
|
|
|
|
|
|
|
for (const noteHistory of protectedNotesHistory) {
|
|
|
|
const decryptedTitle = data_encryption.decrypt(dataKey, noteHistory.note_title);
|
2017-11-19 01:53:17 +08:00
|
|
|
noteHistory.note_title = data_encryption.encrypt(dataKey, "0" + noteHistory.note_history_id, decryptedTitle);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
|
|
|
const decryptedText = data_encryption.decrypt(dataKey, noteHistory.note_text);
|
2017-11-19 01:53:17 +08:00
|
|
|
noteHistory.note_text = data_encryption.encrypt(dataKey, "1" + noteHistory.note_history_id, decryptedText);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
|
|
|
await sql.execute("UPDATE notes SET note_title = ?, note_text = ? WHERE note_id = ?", [noteHistory.note_title, noteHistory.note_text, noteHistory.note_history_id]);
|
|
|
|
}
|
|
|
|
};
|