feat: integrate hash-wasm for enhanced AES encryption and decryption

This commit is contained in:
2026-01-20 09:13:45 -05:00
parent eca6867fca
commit 7e1fb3add4
3 changed files with 117 additions and 47 deletions
+7
View File
@@ -10,6 +10,7 @@
"dependencies": { "dependencies": {
"@lucide/svelte": "^0.562.0", "@lucide/svelte": "^0.562.0",
"@sveltejs/adapter-node": "^5.4.0", "@sveltejs/adapter-node": "^5.4.0",
"hash-wasm": "^4.12.0",
"highlight.js": "^11.8.0", "highlight.js": "^11.8.0",
"qrcode": "^1.5.4" "qrcode": "^1.5.4"
}, },
@@ -2655,6 +2656,12 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/hash-wasm": {
"version": "4.12.0",
"resolved": "https://registry.npmjs.org/hash-wasm/-/hash-wasm-4.12.0.tgz",
"integrity": "sha512-+/2B2rYLb48I/evdOIhP+K/DD2ca2fgBjp6O+GBEnCDk2e4rpeXIK8GvIyRPjTezgmWn9gmKwkQjjx6BtqDHVQ==",
"license": "MIT"
},
"node_modules/hasown": { "node_modules/hasown": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz",
+1
View File
@@ -39,6 +39,7 @@
"dependencies": { "dependencies": {
"@lucide/svelte": "^0.562.0", "@lucide/svelte": "^0.562.0",
"@sveltejs/adapter-node": "^5.4.0", "@sveltejs/adapter-node": "^5.4.0",
"hash-wasm": "^4.12.0",
"highlight.js": "^11.8.0", "highlight.js": "^11.8.0",
"qrcode": "^1.5.4" "qrcode": "^1.5.4"
} }
+109 -47
View File
@@ -1,86 +1,148 @@
import { argon2id } from 'hash-wasm';
function toBase64(bytes: Uint8Array): string {
let binary = '';
const chunkSize = 0x8000;
for (let i = 0; i < bytes.length; i += chunkSize) {
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
}
return btoa(binary);
}
function fromBase64(data: string): Uint8Array {
const binary = atob(data);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}
export async function encryptAES(content: string, password: string): Promise<string> { export async function encryptAES(content: string, password: string): Promise<string> {
const enc = new TextEncoder(); const enc = new TextEncoder();
const keyMaterial = await window.crypto.subtle.importKey( const salt = window.crypto.getRandomValues(new Uint8Array(32));
const memorySize = 65536;
const iterations = 3;
const parallelism = 1;
const keyMaterial = await argon2id({
password,
salt,
iterations,
memorySize,
parallelism,
hashLength: 32,
outputType: 'binary'
});
const key = await window.crypto.subtle.importKey(
'raw', 'raw',
enc.encode(password), keyMaterial as unknown as BufferSource,
{ name: 'PBKDF2' }, { name: 'AES-GCM' },
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, false,
['encrypt'] ['encrypt']
); );
const iv = window.crypto.getRandomValues(new Uint8Array(12)); const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encryptedContent = await window.crypto.subtle.encrypt( const encryptedContent = await window.crypto.subtle.encrypt(
{ {
name: 'AES-GCM', name: 'AES-GCM',
iv: iv iv
}, },
key, key,
enc.encode(content) enc.encode(content)
); );
const combined = new Uint8Array(salt.byteLength + iv.byteLength + encryptedContent.byteLength); const combined = new Uint8Array(salt.byteLength + iv.byteLength + encryptedContent.byteLength);
combined.set(salt, 0); combined.set(salt, 0);
combined.set(iv, salt.byteLength); combined.set(iv, salt.byteLength);
combined.set(new Uint8Array(encryptedContent), salt.byteLength + iv.byteLength); combined.set(new Uint8Array(encryptedContent), salt.byteLength + iv.byteLength);
const result = {
version: 1, return JSON.stringify({
kdf: 'PBKDF2', version: 4,
iterations: 100000, kdf: 'Argon2id',
hash: 'SHA-256', memorySize,
iterations,
parallelism,
algorithm: 'AES-GCM', algorithm: 'AES-GCM',
data: btoa(String.fromCharCode(...combined)) saltLength: salt.byteLength,
}; ivLength: iv.byteLength,
return JSON.stringify(result); data: toBase64(combined)
});
}
export function estimateEncryptedSize(content: string): number {
const rawSize = new TextEncoder().encode(content).length;
const combinedSize = rawSize + 60;
const b64Size = Math.ceil(combinedSize / 3) * 4;
return b64Size + 150;
} }
export async function decryptAES(result: string, password: string): Promise<string | null> { export async function decryptAES(result: string, password: string): Promise<string | null> {
try { try {
const enc = new TextEncoder(); const enc = new TextEncoder();
const parsed = JSON.parse(result); const parsed = JSON.parse(result);
const combined = Uint8Array.from(atob(parsed.data), (c) => c.charCodeAt(0)); const combined = fromBase64(parsed.data);
const salt = combined.slice(0, 16);
const iv = combined.slice(16, 28); const saltLength = parsed.saltLength || 16;
const data = combined.slice(28); const ivLength = parsed.ivLength || 12;
const keyMaterial = await window.crypto.subtle.importKey( const salt = combined.slice(0, saltLength);
'raw', const iv = combined.slice(saltLength, saltLength + ivLength);
enc.encode(password), const data = combined.slice(saltLength + ivLength);
{ name: 'PBKDF2' },
false, let keyMaterial: ArrayBuffer | Uint8Array;
['deriveBits', 'deriveKey']
); if (parsed.kdf === 'Argon2id') {
const key = await window.crypto.subtle.deriveKey( keyMaterial = await argon2id({
{ password,
name: 'PBKDF2', salt,
salt: salt,
iterations: parsed.iterations, iterations: parsed.iterations,
hash: parsed.hash memorySize: parsed.memorySize,
}, parallelism: parsed.parallelism,
keyMaterial, hashLength: 32,
{ name: 'AES-GCM', length: 256 }, outputType: 'binary'
});
} else {
const baseKey = await window.crypto.subtle.importKey(
'raw',
enc.encode(password),
{ name: 'PBKDF2' },
false,
['deriveBits', 'deriveKey']
);
const derivedKey = await window.crypto.subtle.deriveKey(
{
name: 'PBKDF2',
salt,
iterations: parsed.iterations,
hash: parsed.hash
},
baseKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt']
);
keyMaterial = await window.crypto.subtle.exportKey('raw', derivedKey);
}
const key = await window.crypto.subtle.importKey(
'raw',
keyMaterial as unknown as BufferSource,
{ name: 'AES-GCM' },
false, false,
['decrypt'] ['decrypt']
); );
const decryptedContent = await window.crypto.subtle.decrypt( const decryptedContent = await window.crypto.subtle.decrypt(
{ {
name: 'AES-GCM', name: 'AES-GCM',
iv: iv iv
}, },
key, key,
data data
); );
const dec = new TextDecoder();
return dec.decode(decryptedContent); return new TextDecoder().decode(decryptedContent);
} catch { } catch {
return null; return null;
} }