| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  | const tags = { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     1: "<b>", | 
					
						
							|  |  |  |     2: "</b>", | 
					
						
							|  |  |  |     3: "<i>", | 
					
						
							|  |  |  |     4: "</i>", | 
					
						
							|  |  |  |     5: "<u>", | 
					
						
							|  |  |  |     6: "</u>", | 
					
						
							|  |  |  |     9: "<s>", | 
					
						
							|  |  |  |     10: "</s>" | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | let noteChangeDisabled = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  | let isNoteChanged = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | function noteChanged() { | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  |     if (noteChangeDisabled) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  |     isNoteChanged = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function updateNoteFromInputs(note) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     let contents = $('#noteDetail').summernote('code'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  |     html2notecase(contents, note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     let title = $('#noteTitle').val(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  |     getNodeByKey(note.detail.note_id).setTitle(title); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     note.detail.note_title = title; | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function saveNoteToServer(note, callback) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     $.ajax({ | 
					
						
							| 
									
										
										
										
											2017-08-23 21:43:02 -04:00
										 |  |  |         url: baseUrl + 'notes/' + note.detail.note_id, | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         type: 'PUT', | 
					
						
							|  |  |  |         data: JSON.stringify(note), | 
					
						
							|  |  |  |         contentType: "application/json", | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         success: function () { | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  |             isNoteChanged = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             message("Saved!"); | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (callback) { | 
					
						
							|  |  |  |                 callback(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-08-13 21:42:10 -04:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         error: function () { | 
					
						
							| 
									
										
										
										
											2017-08-13 21:42:10 -04:00
										 |  |  |             error("Error saving the note!"); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function saveNoteIfChanged(callback) { | 
					
						
							|  |  |  |     if (!isNoteChanged) { | 
					
						
							|  |  |  |         if (callback) { | 
					
						
							|  |  |  |             callback(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const note = globalNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     updateNoteFromInputs(note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:58:17 -04:00
										 |  |  |     encryptNoteIfNecessary(note).then(note => { | 
					
						
							|  |  |  |         saveNoteToServer(note, callback); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  | setInterval(saveNoteIfChanged, 5000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | $(document).ready(function() { | 
					
						
							|  |  |  |     $("#noteTitle").on('input', function() { | 
					
						
							|  |  |  |         noteChanged(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $('#noteDetail').summernote({ | 
					
						
							|  |  |  |         airMode: true, | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  |         height: 300, | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         callbacks: { | 
					
						
							|  |  |  |             onChange: noteChanged | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-25 19:30:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // so that tab jumps from note title (which has tabindex 1)
 | 
					
						
							|  |  |  |     $(".note-editable").attr("tabindex", 2); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | }); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | var globalNote; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | function createNewTopLevelNote() { | 
					
						
							|  |  |  |     let rootNode = $("#tree").fancytree("getRootNode"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     createNote(rootNode, "root", "into"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 22:36:56 -04:00
										 |  |  | let newNoteCreated = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | function createNote(node, parentKey, target) { | 
					
						
							|  |  |  |     let newNoteName = "new note"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $.ajax({ | 
					
						
							|  |  |  |         url: baseUrl + 'notes/' + parentKey + '/children' , | 
					
						
							|  |  |  |         type: 'POST', | 
					
						
							|  |  |  |         data: JSON.stringify({ | 
					
						
							|  |  |  |             note_title: newNoteName, | 
					
						
							|  |  |  |             target: target, | 
					
						
							|  |  |  |             target_note_id: node.key | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |         contentType: "application/json", | 
					
						
							|  |  |  |         success: function(result) { | 
					
						
							|  |  |  |             let newNode = { | 
					
						
							|  |  |  |                 "title": newNoteName, | 
					
						
							|  |  |  |                 "key": result.note_id, | 
					
						
							|  |  |  |                 "note_id": result.note_id | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 18:50:56 -04:00
										 |  |  |             globalAllNoteIds.push(result.note_id); | 
					
						
							| 
									
										
										
										
											2017-08-27 10:30:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 22:36:56 -04:00
										 |  |  |             newNoteCreated = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |             if (target === 'after') { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                 node.appendSibling(newNode).setActive(true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 node.addChildren(newNode).setActive(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 node.folder = true; | 
					
						
							|  |  |  |                 node.renderTitle(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             message("Created!"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 22:40:54 -04:00
										 |  |  | recentNotes = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  | function setNoteBackgroundIfEncrypted(note) { | 
					
						
							|  |  |  |     if (note.detail.encryption > 0) { | 
					
						
							|  |  |  |         $(".note-editable").addClass("encrypted"); | 
					
						
							| 
									
										
										
										
											2017-09-05 22:01:22 -04:00
										 |  |  |         $("#encryptButton").hide(); | 
					
						
							|  |  |  |         $("#decryptButton").show(); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $(".note-editable").removeClass("encrypted"); | 
					
						
							| 
									
										
										
										
											2017-09-05 22:01:22 -04:00
										 |  |  |         $("#encryptButton").show(); | 
					
						
							|  |  |  |         $("#decryptButton").hide(); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | function loadNote(noteId) { | 
					
						
							|  |  |  |     $.get(baseUrl + 'notes/' + noteId).then(function(note) { | 
					
						
							|  |  |  |         globalNote = note; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $("#noteTitle").val(note.detail.note_title); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 22:36:56 -04:00
										 |  |  |         if (newNoteCreated) { | 
					
						
							|  |  |  |             newNoteCreated = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $("#noteTitle").focus().select(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:58:17 -04:00
										 |  |  |         decryptNoteIfNecessary(note).then(decrypted => { | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             note.detail.note_text = decrypted; | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             let noteText = notecase2html(note); | 
					
						
							| 
									
										
										
										
											2017-08-28 21:41:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             noteChangeDisabled = true; | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             // Clear contents and remove all stored history. This is to prevent undo from going across notes
 | 
					
						
							|  |  |  |             $('#noteDetail').summernote('reset'); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:32:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             $('#noteDetail').summernote('code', noteText); | 
					
						
							| 
									
										
										
										
											2017-08-15 22:32:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             document.location.hash = noteId; | 
					
						
							| 
									
										
										
										
											2017-08-22 22:40:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             $(window).resize(); // to trigger resizing of editor
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             addRecentNote(noteId, note.detail.note_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             noteChangeDisabled = false; | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             setNoteBackgroundIfEncrypted(note); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 10:30:32 -04:00
										 |  |  | function addRecentNote(noteTreeId, noteContentId) { | 
					
						
							| 
									
										
										
										
											2017-08-23 19:57:44 -04:00
										 |  |  |     const origDate = new Date(); | 
					
						
							| 
									
										
										
										
											2017-08-22 22:40:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 19:57:44 -04:00
										 |  |  |     setTimeout(function() { | 
					
						
							|  |  |  |         // we include the note into recent list only if the user stayed on the note at least 5 seconds
 | 
					
						
							| 
									
										
										
										
											2017-08-23 23:42:26 -04:00
										 |  |  |         if (noteTreeId === globalNote.detail.note_id || noteContentId === globalNote.detail.note_id) { | 
					
						
							| 
									
										
										
										
											2017-08-23 19:57:44 -04:00
										 |  |  |             // if it's already there, remove the note
 | 
					
						
							| 
									
										
										
										
											2017-08-27 14:39:26 -04:00
										 |  |  |             c = recentNotes.filter(note => note !== noteTreeId); | 
					
						
							| 
									
										
										
										
											2017-08-23 19:57:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 10:30:32 -04:00
										 |  |  |             recentNotes.unshift(noteTreeId); | 
					
						
							| 
									
										
										
										
											2017-08-23 19:57:44 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }, 1500); | 
					
						
							| 
									
										
										
										
											2017-08-22 22:40:54 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function deriveEncryptionKey(password) { | 
					
						
							|  |  |  |     // why this is done is explained here: https://github.com/ricmoo/scrypt-js - "Encoding notes"
 | 
					
						
							|  |  |  |     const normalizedPassword = password.normalize('NFKC'); | 
					
						
							| 
									
										
										
										
											2017-09-05 21:22:16 -04:00
										 |  |  |     const salt = "dc73b57736511340f132e4b5521d178afa6311c45e0c25e6a9339038507852a6"; | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |     const passwordBuffer = new buffer.SlowBuffer(normalizedPassword); | 
					
						
							|  |  |  |     const saltBuffer = new buffer.SlowBuffer(salt); | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |     // this settings take ~500ms on my laptop
 | 
					
						
							|  |  |  |     const N = 16384, r = 16, p = 1; | 
					
						
							|  |  |  |     // 32 byte key - AES 256
 | 
					
						
							|  |  |  |     const dkLen = 32; | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |     const startedDate = new Date(); | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |     return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |         scrypt(passwordBuffer, saltBuffer, N, r, p, dkLen, function (error, progress, key) { | 
					
						
							|  |  |  |             if (error) { | 
					
						
							|  |  |  |                 console.log("Error: " + error); | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |                 reject(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (key) { | 
					
						
							|  |  |  |                 console.log("Computation took " + (new Date().getTime() - startedDate.getTime()) + "ms"); | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 21:22:16 -04:00
										 |  |  |                 $.ajax({ | 
					
						
							|  |  |  |                     url: baseUrl + 'password/verify', | 
					
						
							|  |  |  |                     type: 'POST', | 
					
						
							|  |  |  |                     data: JSON.stringify({ | 
					
						
							|  |  |  |                         password: sha256(key) | 
					
						
							|  |  |  |                     }), | 
					
						
							|  |  |  |                     contentType: "application/json", | 
					
						
							|  |  |  |                     success: function (result) { | 
					
						
							|  |  |  |                         if (result.valid) { | 
					
						
							|  |  |  |                             resolve(key); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         else { | 
					
						
							|  |  |  |                             alert("Wrong password"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             reject(); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 }); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 // update UI with progress complete
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | let globalEncryptionKeyPromise = null; | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function getEncryptionKey() { | 
					
						
							|  |  |  |     if (globalEncryptionKeyPromise === null) { | 
					
						
							|  |  |  |         const password = prompt("Enter password for encryption"); | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         globalEncryptionKeyPromise = deriveEncryptionKey(password); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |     return globalEncryptionKeyPromise; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function getAes() { | 
					
						
							|  |  |  |     return getEncryptionKey().then(encryptionKey => { | 
					
						
							|  |  |  |         return new aesjs.ModeOfOperation.ctr(encryptionKey, new aesjs.Counter(5)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:58:17 -04:00
										 |  |  | function encryptNoteIfNecessary(note) { | 
					
						
							|  |  |  |     if (note.detail.encryption === 0) { | 
					
						
							|  |  |  |         return Promise.resolve(note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return encryptNote(note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:58:17 -04:00
										 |  |  | function encryptNote(note) { | 
					
						
							|  |  |  |     return getAes().then(aes => { | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         const noteJson = note.detail.note_text; | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         const noteBytes = aesjs.utils.utf8.toBytes(noteJson); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const encryptedBytes = aes.encrypt(noteBytes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // To print or store the binary data, you may convert it to hex
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:58:17 -04:00
										 |  |  |         note.detail.note_text = uint8ToBase64(encryptedBytes); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:58:17 -04:00
										 |  |  |         return note; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function encryptNoteAndSendToServer() { | 
					
						
							|  |  |  |     updateNoteFromInputs(globalNote); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     encryptNote(globalNote).then(note => { | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         note.detail.encryption = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         saveNoteToServer(note); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         setNoteBackgroundIfEncrypted(note); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 22:01:22 -04:00
										 |  |  | function decryptNoteAndSendToServer() { | 
					
						
							|  |  |  |     const note = globalNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     updateNoteFromInputs(note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     note.detail.encryption = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     saveNoteToServer(note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setNoteBackgroundIfEncrypted(note); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:58:17 -04:00
										 |  |  | 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function decryptNote(encryptedBase64) { | 
					
						
							|  |  |  |     return getAes().then(aes => { | 
					
						
							|  |  |  |         const encryptedBytes = base64ToUint8Array(encryptedBase64); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const decryptedBytes = aes.decrypt(encryptedBytes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return aesjs.utils.utf8.fromBytes(decryptedBytes); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function uint8ToBase64(u8Arr) { | 
					
						
							|  |  |  |     const CHUNK_SIZE = 0x8000; //arbitrary number
 | 
					
						
							|  |  |  |     const length = u8Arr.length; | 
					
						
							|  |  |  |     let index = 0; | 
					
						
							|  |  |  |     let result = ''; | 
					
						
							|  |  |  |     let slice; | 
					
						
							|  |  |  |     while (index < length) { | 
					
						
							|  |  |  |         slice = u8Arr.subarray(index, Math.min(index + CHUNK_SIZE, length)); | 
					
						
							|  |  |  |         result += String.fromCharCode.apply(null, slice); | 
					
						
							|  |  |  |         index += CHUNK_SIZE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return btoa(result); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-08-21 20:34:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function base64ToUint8Array(base64encoded) { | 
					
						
							|  |  |  |     return new Uint8Array(atob(base64encoded).split("").map(function(c) { return c.charCodeAt(0); })); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | } |