diff --git a/SharpbinV3.Client/src/lib/utils/encryption.ts b/SharpbinV3.Client/src/lib/utils/encryption.ts new file mode 100644 index 0000000..120b1dc --- /dev/null +++ b/SharpbinV3.Client/src/lib/utils/encryption.ts @@ -0,0 +1,87 @@ +export async function encryptAES(content: string, password: string): Promise { + 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); +} + +export async function decryptAES(result: string, password: string): Promise { + try { + const enc = new TextEncoder(); + const parsed = JSON.parse(result); + const combined = Uint8Array.from(atob(parsed.data), c => c.charCodeAt(0)); + const salt = combined.slice(0, 16); + const iv = combined.slice(16, 28); + const data = combined.slice(28); + const keyMaterial = await window.crypto.subtle.importKey( + 'raw', + enc.encode(password), + { name: 'PBKDF2' }, + false, + ['deriveBits', 'deriveKey'] + ); + const key = await window.crypto.subtle.deriveKey( + { + name: 'PBKDF2', + salt: salt, + iterations: parsed.iterations, + hash: parsed.hash + }, + keyMaterial, + { name: 'AES-GCM', length: 256 }, + false, + ['decrypt'] + ); + const decryptedContent = await window.crypto.subtle.decrypt( + { + name: 'AES-GCM', + iv: iv + }, + key, + data + ); + const dec = new TextDecoder(); + return dec.decode(decryptedContent); + } catch { + return null; + } +} \ No newline at end of file diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte index 2744857..1fa6189 100644 --- a/SharpbinV3.Client/src/routes/+page.svelte +++ b/SharpbinV3.Client/src/routes/+page.svelte @@ -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 { - 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); - }