feat: implement decryptAes function; moved to encryption.ts

This commit is contained in:
2025-12-29 04:26:58 -05:00
parent 49b9673d48
commit f61f31de0e
2 changed files with 93 additions and 46 deletions
+6 -46
View File
@@ -4,6 +4,7 @@
import { user } from '$lib/stores/user';
import { getToken } from '$lib/utils/auth';
import { fly } from 'svelte/transition';
import { encryptAES } from '$lib/utils/encryption';
import Dropdown from '$lib/components/Dropdown.svelte';
export let data: PageData;
@@ -72,6 +73,11 @@
const response = await fetch(url, options);
if (response.ok) {
const result = await response.json();
if (selectedVisibility === 2) {
const passwordBase64 = btoa(password);
window.location.href = `/${result.id}#${passwordBase64}`;
return;
}
window.location.href = `/${result.id}`;
} else {
error = 'Failed to create paste. Please try again.';
@@ -80,52 +86,6 @@
}, 5000);
}
}
async function encryptAES(content: string, password: string): Promise<string> {
const enc = new TextEncoder();
const keyMaterial = await window.crypto.subtle.importKey(
'raw',
enc.encode(password),
{ name: 'PBKDF2' },
false,
['deriveBits', 'deriveKey']
);
const salt = window.crypto.getRandomValues(new Uint8Array(16));
const key = await window.crypto.subtle.deriveKey(
{
name: 'PBKDF2',
salt: salt,
iterations: 100000,
hash: 'SHA-256'
},
keyMaterial,
{ name: 'AES-GCM', length: 256 },
false,
['encrypt']
);
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encryptedContent = await window.crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: iv
},
key,
enc.encode(content)
);
const combined = new Uint8Array(salt.byteLength + iv.byteLength + encryptedContent.byteLength);
combined.set(salt, 0);
combined.set(iv, salt.byteLength);
combined.set(new Uint8Array(encryptedContent), salt.byteLength + iv.byteLength);
const result = {
version: 1,
kdf: 'PBKDF2',
iterations: 100000,
hash: 'SHA-256',
algorithm: 'AES-GCM',
data: btoa(String.fromCharCode(...combined))
};
return JSON.stringify(result);
}
</script>
<main