From 13b8255055fc657c0f9744153d8abb5518287175 Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Tue, 6 May 2025 17:07:10 -0400 Subject: [PATCH] Work on encryption and other clean up. --- .../ae_comp__journal_entry_obj_id_view.svelte | 70 ++-- .../journals/ae_comp__obj_core_props.svelte | 322 ++++++++++++++++++ 2 files changed, 363 insertions(+), 29 deletions(-) create mode 100644 src/routes/journals/ae_comp__obj_core_props.svelte diff --git a/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte b/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte index 08f721ee..12188670 100644 --- a/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte +++ b/src/routes/journals/ae_comp__journal_entry_obj_id_view.svelte @@ -2,7 +2,7 @@ // *** Import Svelte specific import { goto } from '$app/navigation'; // import { clipboard } from '@skeletonlabs/skeleton'; - +import { marked } from 'marked'; import { ArrowDown01, ArrowDown10, ArrowDownUp, BookHeart, BriefcaseBusiness, @@ -12,7 +12,7 @@ import { Globe, Group, History, LockKeyhole, LockKeyholeOpen, - MessageSquareWarning, Minus, + MessageSquareWarning, Menu, Minus, NotebookPen, NotebookText, NotepadTextDashed, Pencil, PenLine, Plus, RemoveFormatting, @@ -193,7 +193,7 @@ async function update_journal_entry() { decrypted_history = tmp_entry_obj?.history; } - data_kv.history = decrypted_history + '\n' + cut_out_string; + data_kv.history = decrypted_history ?? '' + '\n' + cut_out_string; data_kv.history_encrypted = null; decrypted_history = ''; } else if (tmp_entry_obj?.private) { @@ -211,7 +211,7 @@ async function update_journal_entry() { decrypted_history = await handle_decrypt_string(tmp_entry_obj?.history_encrypted, journal_key); if (tmp_entry_obj?.history) { - data_kv.history = tmp_entry_obj?.history + '\n' + decrypted_history + '\n' + cut_out_string; + data_kv.history = tmp_entry_obj?.history ?? '' + '\n' + decrypted_history + '\n' + cut_out_string; } else { data_kv.history = decrypted_history + '\n' + cut_out_string; } @@ -284,12 +284,12 @@ async function update_journal_entry() { // // // Encrypt the content // // let encrypted_base64 = await ae_util.encrypt_content(content, journal_key); - // // encrypted_base64_content = encrypted_base64.base64; + // // encrypted_base64_string = encrypted_base64.base64; // // encryption_iv = encrypted_base64.iv; - // // console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_content}`); + // // console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`); // // // Combine the IV and encrypted content - // // const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_content; + // // const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_string; // let combined_data = await handle_encrypt_string(content, journal_key); @@ -405,7 +405,7 @@ let journal_key = $derived(() => { }); // console.log('TEST: journal_key', journal_key); let content = ''; // "This is my test content to encrypt and decrypt."; -let encrypted_base64_content: string = $state(''); +let encrypted_base64_string: string = $state(''); let encryption_iv: null|Uint8Array = $state(null); let decrypted_content: string = $state(''); let trigger_decrypt: boolean = $state(false); @@ -431,10 +431,10 @@ $effect(() => { // // content = tmp_entry_obj?.content; // // let encrypted_base64 = await ae_util.encrypt_content(content, journal_key); -// // encrypted_base64_content = encrypted_base64.base64; +// // encrypted_base64_string = encrypted_base64.base64; // // encryption_iv = encrypted_base64.iv; -// // let decrypted = await ae_util.decrypt_content(encrypted_base64_content, encryption_iv, journal_key); +// // let decrypted = await ae_util.decrypt_content(encrypted_base64_string, encryption_iv, journal_key); // // decrypted_content = decrypted; // // if (log_lvl) { // // console.log('Decrypted content:', decrypted_content); @@ -450,21 +450,21 @@ $effect(() => { // console.log('TEST: handle_decrypt_content'); // } // let combined_data = tmp_entry_obj?.content_encrypted; -// let [encryption_iv_hex, encrypted_base64_content] = combined_data.split(':'); +// let [encryption_iv_hex, encrypted_base64_string] = combined_data.split(':'); // encryption_iv = new Uint8Array(encryption_iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); // if (log_lvl) { -// console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_content}`); +// console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`); // } // let decrypted: string|null = null; // try { -// decrypted = await ae_util.decrypt_content(encrypted_base64_content, encryption_iv, journal_key); +// decrypted = await ae_util.decrypt_content(encrypted_base64_string, encryption_iv, journal_key); // } catch (error) { // console.error('Error decrypting content:', error); // alert('Failed to decrypt content. Please check the passcode.'); // return; // } -// // let decrypted = await ae_util.decrypt_content(encrypted_base64_content, encryption_iv, journal_key); +// // let decrypted = await ae_util.decrypt_content(encrypted_base64_string, encryption_iv, journal_key); // // decrypted_content = 'XXX '+decrypted+' XXX'; // if (!decrypted) { @@ -497,21 +497,24 @@ async function handle_decrypt_string(encrypted_string: string, passcode: string) } let combined_data = encrypted_string; - let [encryption_iv_hex, encrypted_base64_content] = combined_data.split(':'); + let [encryption_iv_hex, encrypted_base64_string] = combined_data.split(':'); encryption_iv = new Uint8Array(encryption_iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); if (log_lvl) { - console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_content}`); + console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`); } // Decrypt the string using the journal key let decrypted_string = ''; try { - decrypted_string = await ae_util.decrypt_content(encrypted_base64_content, encryption_iv, passcode); + decrypted_string = await ae_util.decrypt_content(encrypted_base64_string, encryption_iv, passcode); } catch (error) { - console.error('Error decrypting content:', error); - alert('Failed to decrypt content. Please check the passcode.'); + console.error('Error decrypting string:', error); + alert('Failed to decrypt string. Please check the passcode.'); return; } + if (log_lvl) { + console.log('Decrypted string:', decrypted_string); + } return decrypted_string; } @@ -531,12 +534,12 @@ async function handle_encrypt_string(text_string: string, passcode: string) { // Encrypt the string using the journal key let encrypted_base64 = await ae_util.encrypt_content(text_string, passcode); - let encrypted_base64_content = encrypted_base64.base64; + let encrypted_base64_string = encrypted_base64.base64; let encryption_iv = encrypted_base64.iv; - console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_content}`); + console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_string}`); // Combine the IV and encrypted content - const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_content; + const combined_data = Array.from(encryption_iv).map(byte => byte.toString(16).padStart(2, '0')).join('') + ':' + encrypted_base64_string; return combined_data; } @@ -636,7 +639,7 @@ function handle_cut_string(old_string: string) { > {#if $journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id]} - + {:else} {/if} @@ -1196,7 +1199,7 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} --> }} --> - + {:else if ($journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id])} @@ -1362,24 +1365,33 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} --> } if (tmp_entry_obj?.history_encrypted) { + let history_cleaned: null|string = null; + let history_md_html: null|string = null; + decrypted_history = await handle_decrypt_string(tmp_entry_obj.history_encrypted, journal_key); console.log('Decrypted history:', decrypted_history); tmp_entry_obj.history = decrypted_history; + + history_cleaned = decrypted_history.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,""); + history_md_html = await marked.parse(history_cleaned ?? '') ?? null; + tmp_entry_obj.history_md_html = history_md_html; + // console.log('History cleaned:', history_cleaned); + console.log('History md html:', history_md_html); } }} class="btn btn-sm variant-soft-secondary hover:variant-filled-secondary *:hover:inline lg:text-xs" title="Toggle edit mode for history of this journal entry" > {#if $journals_sess.show__content__journal_entry_history == 'view'} - - {:else if $journals_sess.show__content__journal_entry_history == 'edit'} + {:else if $journals_sess.show__content__journal_entry_history == 'edit'} + {/if} @@ -1419,7 +1431,7 @@ tabindex={$ae_loc.edit_mode ? 0 : -1} --> prose-li:m-0 prose-li:p-0 prose-li:line-height-none " > - {@html $lq__journal_entry_obj?.history_md_html} + {@html tmp_entry_obj?.history_md_html} {:else if $journals_sess?.show__content__journal_entry_history == 'edit'}