fixed update of encrypted note

This commit is contained in:
azivner 2017-09-04 21:58:17 -04:00
parent b2338c31d7
commit 53d327e435
2 changed files with 36 additions and 19 deletions

View file

@ -42,7 +42,7 @@
<div style="float: left; margin: 0 5px 5px 5px;" class="hide-toggle">
<input autocomplete="off" value="Welcome to Notecase web app!" id="noteTitle" style="font-size: x-large; border: 0; width: 600px;" tabindex="1">
<button class="btn btn-sm" onclick="encryptNote();">Encrypt</button>
<button class="btn btn-sm" onclick="encryptNoteAndSendToServer();">Encrypt</button>
</div>
<div style="clear: both; height: 0"></div>

View file

@ -67,7 +67,9 @@ function saveNoteIfChanged(callback) {
updateNoteFromInputs(note);
saveNoteToServer(note, callback);
encryptNoteIfNecessary(note).then(note => {
saveNoteToServer(note, callback);
});
}
setInterval(saveNoteIfChanged, 5000);
@ -160,16 +162,7 @@ function loadNote(noteId) {
$("#noteTitle").focus().select();
}
let decryptPromise;
if (note.detail.encryption === 1) {
decryptPromise = decryptNote(note.detail.note_text);
}
else {
decryptPromise = Promise.resolve(note.detail.note_text);
}
decryptPromise.then(decrypted => {
decryptNoteIfNecessary(note).then(decrypted => {
note.detail.note_text = decrypted;
let noteText = notecase2html(note);
@ -262,12 +255,17 @@ function getAes() {
});
}
function encryptNote() {
getAes().then(aes => {
const note = globalNote;
updateNoteFromInputs(note);
function encryptNoteIfNecessary(note) {
if (note.detail.encryption === 0) {
return Promise.resolve(note);
}
else {
return encryptNote(note);
}
}
function encryptNote(note) {
return getAes().then(aes => {
const noteJson = note.detail.note_text;
const noteBytes = aesjs.utils.utf8.toBytes(noteJson);
@ -275,9 +273,16 @@ function encryptNote() {
const encryptedBytes = aes.encrypt(noteBytes);
// To print or store the binary data, you may convert it to hex
const encryptedBase64 = uint8ToBase64(encryptedBytes);
note.detail.note_text = uint8ToBase64(encryptedBytes);
note.detail.note_text = encryptedBase64;
return note;
});
}
function encryptNoteAndSendToServer() {
updateNoteFromInputs(globalNote);
encryptNote(globalNote).then(note => {
note.detail.encryption = 1;
saveNoteToServer(note);
@ -286,6 +291,18 @@ function encryptNote() {
});
}
function decryptNoteIfNecessary(note) {
let decryptPromise;
if (note.detail.encryption === 1) {
decryptPromise = decryptNote(note.detail.note_text);
}
else {
decryptPromise = Promise.resolve(note.detail.note_text);
}
return decryptPromise;
}
function decryptNote(encryptedBase64) {
return getAes().then(aes => {
const encryptedBytes = base64ToUint8Array(encryptedBase64);