Now with first version of encryption!

This commit is contained in:
Scott Idem
2025-04-26 12:27:44 -04:00
parent 9bfb6580f2
commit 6314d8f6e6
3 changed files with 112 additions and 0 deletions

View File

@@ -209,6 +209,74 @@ async function change_journal_id() {
$journals_slct.journal_id = tmp_entry_obj?.journal_id;
goto(`/journals/${tmp_entry_obj?.journal_id}`);
}
// async function generate_iv() {
// const data = new Uint8Array(16);
// crypto.getRandomValues(data);
// return data;
// }
// async function encrypt_content(content, keyData) {
// const iv = await generate_iv();
// const keyBytes = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(keyData));
// const key = await crypto.subtle.importKey('raw', keyBytes, { name: 'AES-CBC' }, false, ['encrypt']);
// const encodedContent = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, key, new TextEncoder().encode(content));
// const base64 = btoa(String.fromCharCode(...new Uint8Array(encodedContent)));
// console.log('Base64 Encoded Content:', base64);
// return { base64, iv };
// }
// async function decrypt_content(base64Content, iv, keyData) {
// const keyBytes = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(keyData));
// const key = await crypto.subtle.importKey('raw', keyBytes, { name: 'AES-CBC' }, false, ['decrypt']);
// const encryptedContent = Uint8Array.from(atob(base64Content), c => c.charCodeAt(0));
// const decryptedContent = await crypto.subtle.decrypt({ name: 'AES-CBC', iv }, key, encryptedContent);
// const decodedContent = new TextDecoder().decode(decryptedContent);
// console.log('Decrypted Content:', decodedContent);
// return decodedContent;
// }
// Example usage
(async () => {
const keyData = "my-secret-key";
const content = "This is my test content to encrypt and decrypt.";
const { base64, iv } = await ae_util.encrypt_content(content, keyData);
await ae_util.decrypt_content(base64, iv, keyData);
})();
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<ArrayBuffer> = $state(null);
let test_decrypted: string = $state('');
$effect(async () => {
if ($lq__journal_entry_obj?.content) {
content = $lq__journal_entry_obj?.content;
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;
if (log_lvl) {
console.log('Decrypted content:', test_decrypted);
}
}
// console.log('Encrypted content:', base64);
// console.log('IV:', iv);
});
</script>
@@ -706,6 +774,17 @@ async function change_journal_id() {
>
{@html $lq__journal_entry_obj?.content_md_html}
</article>
<!-- {@html async () => {
const { base64, iv } = await encrypt_content(content, test_key);
// console.log('Encrypted content:', base64);
// console.log('IV:', iv);
const decryptedContent = await decrypt_content(base64, iv, test_key);
// console.log('Decrypted content:', decryptedContent);
return base64;
}} -->
<!-- {@html encrypt_content($lq__journal_entry_obj?.content, test_key)} -->
<!-- --{@html test_encrypted_base64}-- -->
<!-- {@html marked.parse($lq__journal_entry_obj?.content)} -->
</div>