diff --git a/src/lib/ae_journals/ae_journals__journal_entry.ts b/src/lib/ae_journals/ae_journals__journal_entry.ts index 1e315015..f72a4e7a 100644 --- a/src/lib/ae_journals/ae_journals__journal_entry.ts +++ b/src/lib/ae_journals/ae_journals__journal_entry.ts @@ -385,6 +385,7 @@ export async function db_save_ae_obj_li__journal_entry( // content_md_html_alt: content_md_html_alt, content_html: obj.content_html, content_json: obj.content_json, + content_encrypted: obj.content_encrypted, // url: obj.url, // url_text: obj.url_text, diff --git a/src/lib/ae_journals/db_journals.ts b/src/lib/ae_journals/db_journals.ts index 22f30b8d..99a673bd 100644 --- a/src/lib/ae_journals/db_journals.ts +++ b/src/lib/ae_journals/db_journals.ts @@ -194,6 +194,7 @@ export interface Journal_Entry { content_md_html_alt?: null|string; // Markdown converted to HTML based on content. Uses marked or similar library for conversion. content_html?: null|string; content_json?: null|string; + content_encrypted?: null|string; // This is the encrypted content of the journal entry start_datetime?: null|Date; end_datetime?: null|Date; 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 2332af2c..0bc9eb74 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 @@ -142,7 +142,7 @@ async function update_journal_entry() { return; } - let data_kv = { + let data_kv: key_val = { alert: tmp_entry_obj?.alert, personal: tmp_entry_obj?.personal, private: tmp_entry_obj?.private, @@ -164,6 +164,21 @@ async function update_journal_entry() { tags: tmp_entry_obj?.tags, }; + if (tmp_entry_obj?.content && tmp_entry_obj?.private) { + content = $lq__journal_entry_obj?.content; + + // Encrypt the content + let encrypted_base64 = await ae_util.encrypt_content(content, test_key); + encrypted_base64_content = encrypted_base64.base64; + encryption_iv = encrypted_base64.iv; + console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_content}`); + + // 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; + + data_kv.content_encrypted = combined_data; + } + // Call API to save the content try { await journals_func.update_ae_obj__journal_entry({ @@ -248,35 +263,49 @@ async function change_journal_id() { const test_key = "my-secret-key"; let content = "This is my test content to encrypt and decrypt."; -let test_encrypted_base64: string = $state(''); -let test_iv: null|Uint8Array = $state(null); -let test_decrypted: string = $state(''); +let encrypted_base64_content: string = $state(''); +let encryption_iv: null|Uint8Array = $state(null); +let decrypted_content: string = $state(''); +let trigger_decrypt: boolean = $state(false); $effect(async () => { - if ($lq__journal_entry_obj?.content) { - content = $lq__journal_entry_obj?.content; + if (tmp_entry_obj?.content_encrypted && trigger_decrypt) { + log_lvl = 1; - let encrypted_base64 = await ae_util.encrypt_content(content, test_key); - test_encrypted_base64 = encrypted_base64.base64; - test_iv = encrypted_base64.iv; - // test_encrypted = base64; - - // base64.then((result) => { - // test_encrypted = result; - // console.log('Encrypted content:', test_encrypted); - // }).catch((error) => { - // console.error('Error encrypting content:', error); - // }); - - let decrypted = await ae_util.decrypt_content(test_encrypted_base64, test_iv, test_key); - test_decrypted = decrypted; + trigger_decrypt = false; + let combined_data = tmp_entry_obj?.content_encrypted; + let [encryption_iv_hex, encrypted_base64_content] = combined_data.split(':'); + encryption_iv = new Uint8Array(encryption_iv_hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16))); if (log_lvl) { - console.log('Decrypted content:', test_decrypted); + console.log(`IV: ${encryption_iv}; Encrypted: ${encrypted_base64_content}`); } + + let decrypted = await ae_util.decrypt_content(encrypted_base64_content, encryption_iv, test_key); + decrypted_content = decrypted; + if (log_lvl) { + console.log('Decrypted content:', decrypted_content); + } + + // tmp_entry_obj.content_encrypted = null; } + + // if (tmp_entry_obj?.content) { + // content = tmp_entry_obj?.content; + + // let encrypted_base64 = await ae_util.encrypt_content(content, test_key); + // encrypted_base64_content = encrypted_base64.base64; + // encryption_iv = encrypted_base64.iv; + + // let decrypted = await ae_util.decrypt_content(encrypted_base64_content, encryption_iv, test_key); + // decrypted_content = decrypted; + // if (log_lvl) { + // console.log('Decrypted content:', decrypted_content); + // } + // } // console.log('Encrypted content:', base64); // console.log('IV:', iv); }); + @@ -736,6 +765,10 @@ $effect(async () => { $journals_loc.entry.edit = !$journals_loc.entry.edit; $journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id] = $journals_loc.entry.edit; } + + if ($ae_loc.trusted_access && $journals_loc.entry.edit_kv[$lq__journal_entry_obj?.journal_entry_id]) { + trigger_decrypt = true; + } }} role={$ae_loc.edit_mode ? 'button' : 'article'} tabindex={$ae_loc.edit_mode ? 0 : -1} @@ -772,7 +805,7 @@ $effect(async () => { " id="rendered_journal_entry_content_{$lq__journal_entry_obj?.journal_entry_id}" > - {@html $lq__journal_entry_obj?.content_md_html} + {@html decrypted_content ?? $lq__journal_entry_obj?.content_md_html} - +