mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	note titles are now encrypted as well - plus auto-decryption of note tree and unloading
This commit is contained in:
		| @@ -12,6 +12,7 @@ def getTree(): | |||||||
|                        "notes_tree.*, " |                        "notes_tree.*, " | ||||||
|                        "COALESCE(clone.note_title, notes.note_title) as note_title, " |                        "COALESCE(clone.note_title, notes.note_title) as note_title, " | ||||||
|                        "notes.note_clone_id, " |                        "notes.note_clone_id, " | ||||||
|  |                        "notes.encryption, " | ||||||
|                        "case when notes.note_clone_id is null or notes.note_clone_id = '' then 0 else 1 end as is_clone " |                        "case when notes.note_clone_id is null or notes.note_clone_id = '' then 0 else 1 end as is_clone " | ||||||
|                        "from notes_tree " |                        "from notes_tree " | ||||||
|                        "join notes on notes.note_id = notes_tree.note_id " |                        "join notes on notes.note_id = notes_tree.note_id " | ||||||
|   | |||||||
| @@ -91,6 +91,16 @@ $("#encryptionPasswordForm").submit(function() { | |||||||
|  |  | ||||||
|         globalEncryptionKey = key; |         globalEncryptionKey = key; | ||||||
|  |  | ||||||
|  |         for (const noteId of globalAllNoteIds) { | ||||||
|  |             const note = getNodeByKey(noteId); | ||||||
|  |  | ||||||
|  |             if (note.data.encryption > 0) { | ||||||
|  |                 const title = decryptString(note.data.note_title); | ||||||
|  |  | ||||||
|  |                 note.setTitle(title); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         if (globalEncryptionCallback !== null) { |         if (globalEncryptionCallback !== null) { | ||||||
|             globalEncryptionCallback(); |             globalEncryptionCallback(); | ||||||
|  |  | ||||||
| @@ -110,6 +120,14 @@ function getAes() { | |||||||
|  |  | ||||||
|             if (globalNote.detail.encryption > 0) { |             if (globalNote.detail.encryption > 0) { | ||||||
|                 loadNote(globalNote.detail.note_id); |                 loadNote(globalNote.detail.note_id); | ||||||
|  |  | ||||||
|  |                 for (const noteId of globalAllNoteIds) { | ||||||
|  |                     const note = getNodeByKey(noteId); | ||||||
|  |  | ||||||
|  |                     if (note.data.encryption > 0) { | ||||||
|  |                         note.setTitle("[encrypted]"); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     }, globalEncryptionKeyTimeToLive + 1000); |     }, globalEncryptionKeyTimeToLive + 1000); | ||||||
| @@ -126,16 +144,29 @@ function encryptNoteIfNecessary(note) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| function encryptNote(note) { | function encryptString(str) { | ||||||
|     const aes = getAes(); |     const aes = getAes(); | ||||||
|     const noteJson = note.detail.note_text; |     const bytes = aesjs.utils.utf8.toBytes(str); | ||||||
|  |  | ||||||
|     const noteBytes = aesjs.utils.utf8.toBytes(noteJson); |     const encryptedBytes = aes.encrypt(bytes); | ||||||
|  |  | ||||||
|     const encryptedBytes = aes.encrypt(noteBytes); |     return uint8ToBase64(encryptedBytes); | ||||||
|  | } | ||||||
|  |  | ||||||
|     // To print or store the binary data, you may convert it to hex | function decryptString(encryptedBase64) { | ||||||
|     note.detail.note_text = uint8ToBase64(encryptedBytes); |     const aes = getAes(); | ||||||
|  |     const encryptedBytes = base64ToUint8Array(encryptedBase64); | ||||||
|  |  | ||||||
|  |     const decryptedBytes = aes.decrypt(encryptedBytes); | ||||||
|  |  | ||||||
|  |     return aesjs.utils.utf8.fromBytes(decryptedBytes); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function encryptNote(note) { | ||||||
|  |     note.detail.note_title = encryptString(note.detail.note_title); | ||||||
|  |     note.detail.note_text = encryptString(note.detail.note_text); | ||||||
|  |  | ||||||
|  |     note.detail.encryption = 1; | ||||||
|  |  | ||||||
|     return note; |     return note; | ||||||
| } | } | ||||||
| @@ -148,8 +179,6 @@ function encryptNoteAndSendToServer() { | |||||||
|  |  | ||||||
|         encryptNote(note); |         encryptNote(note); | ||||||
|  |  | ||||||
|         note.detail.encryption = 1; |  | ||||||
|  |  | ||||||
|         saveNoteToServer(note); |         saveNoteToServer(note); | ||||||
|  |  | ||||||
|         setNoteBackgroundIfEncrypted(note); |         setNoteBackgroundIfEncrypted(note); | ||||||
| @@ -171,19 +200,12 @@ function decryptNoteAndSendToServer() { | |||||||
| } | } | ||||||
|  |  | ||||||
| function decryptNoteIfNecessary(note) { | function decryptNoteIfNecessary(note) { | ||||||
|     if (note.detail.encryption === 1) { |     if (note.detail.encryption > 0) { | ||||||
|         return decryptNote(note.detail.note_text); |         return decryptNote(note); | ||||||
|     } |  | ||||||
|     else { |  | ||||||
|         return note.detail.note_text; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| function decryptNote(encryptedBase64) { | function decryptNote(note) { | ||||||
|     const aes = getAes(); |     note.detail.note_title = decryptString(note.detail.note_title); | ||||||
|     const encryptedBytes = base64ToUint8Array(encryptedBase64); |     note.detail.note_text = decryptString(note.detail.note_text); | ||||||
|  |  | ||||||
|     const decryptedBytes = aes.decrypt(encryptedBytes); |  | ||||||
|  |  | ||||||
|     return aesjs.utils.utf8.fromBytes(decryptedBytes); |  | ||||||
| } | } | ||||||
| @@ -158,8 +158,6 @@ function loadNote(noteId) { | |||||||
|     $.get(baseUrl + 'notes/' + noteId).then(function(note) { |     $.get(baseUrl + 'notes/' + noteId).then(function(note) { | ||||||
|         globalNote = note; |         globalNote = note; | ||||||
|  |  | ||||||
|         $("#noteTitle").val(note.detail.note_title); |  | ||||||
|  |  | ||||||
|         if (newNoteCreated) { |         if (newNoteCreated) { | ||||||
|             newNoteCreated = false; |             newNoteCreated = false; | ||||||
|  |  | ||||||
| @@ -177,7 +175,9 @@ function loadNote(noteId) { | |||||||
|  |  | ||||||
|             $("#encryptionPassword").val(''); |             $("#encryptionPassword").val(''); | ||||||
|  |  | ||||||
|             note.detail.note_text = decryptNoteIfNecessary(note); |             decryptNoteIfNecessary(note); | ||||||
|  |  | ||||||
|  |             $("#noteTitle").val(note.detail.note_title); | ||||||
|  |  | ||||||
|             let noteText = notecase2html(note); |             let noteText = notecase2html(note); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -164,10 +164,15 @@ $(function(){ | |||||||
|             for (const note of notes) { |             for (const note of notes) { | ||||||
|                 globalAllNoteIds.push(note.note_id); |                 globalAllNoteIds.push(note.note_id); | ||||||
|  |  | ||||||
|                 note.title = note.note_title; |                 if (note.encryption > 0) { | ||||||
|  |                     note.title = "[encrypted]"; | ||||||
|  |                 } | ||||||
|  |                 else { | ||||||
|  |                     note.title = note.note_title; | ||||||
|  |  | ||||||
|                 if (note.is_clone) { |                     if (note.is_clone) { | ||||||
|                     note.title += " (clone)"; |                         note.title += " (clone)"; | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 note.key = note.note_id; |                 note.key = note.note_id; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user