mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: implement decryptAes function; moved to encryption.ts
This commit is contained in:
@@ -0,0 +1,87 @@
|
|||||||
|
export 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function decryptAES(result: string, password: string): Promise<string | null> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
import { user } from '$lib/stores/user';
|
import { user } from '$lib/stores/user';
|
||||||
import { getToken } from '$lib/utils/auth';
|
import { getToken } from '$lib/utils/auth';
|
||||||
import { fly } from 'svelte/transition';
|
import { fly } from 'svelte/transition';
|
||||||
|
import { encryptAES } from '$lib/utils/encryption';
|
||||||
import Dropdown from '$lib/components/Dropdown.svelte';
|
import Dropdown from '$lib/components/Dropdown.svelte';
|
||||||
|
|
||||||
export let data: PageData;
|
export let data: PageData;
|
||||||
@@ -72,6 +73,11 @@
|
|||||||
const response = await fetch(url, options);
|
const response = await fetch(url, options);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
if (selectedVisibility === 2) {
|
||||||
|
const passwordBase64 = btoa(password);
|
||||||
|
window.location.href = `/${result.id}#${passwordBase64}`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
window.location.href = `/${result.id}`;
|
window.location.href = `/${result.id}`;
|
||||||
} else {
|
} else {
|
||||||
error = 'Failed to create paste. Please try again.';
|
error = 'Failed to create paste. Please try again.';
|
||||||
@@ -80,52 +86,6 @@
|
|||||||
}, 5000);
|
}, 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>
|
</script>
|
||||||
|
|
||||||
<main
|
<main
|
||||||
|
|||||||
Reference in New Issue
Block a user