From 7e1fb3add4f438a206638ea0c90b0a27fd108c53 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Tue, 20 Jan 2026 09:13:45 -0500 Subject: [PATCH] feat: integrate hash-wasm for enhanced AES encryption and decryption --- SharpbinV3.Client/package-lock.json | 7 + SharpbinV3.Client/package.json | 1 + SharpbinV3.Client/src/lib/utils/encryption.ts | 156 ++++++++++++------ 3 files changed, 117 insertions(+), 47 deletions(-) diff --git a/SharpbinV3.Client/package-lock.json b/SharpbinV3.Client/package-lock.json index 48f8667..49ea5dd 100644 --- a/SharpbinV3.Client/package-lock.json +++ b/SharpbinV3.Client/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "@lucide/svelte": "^0.562.0", "@sveltejs/adapter-node": "^5.4.0", + "hash-wasm": "^4.12.0", "highlight.js": "^11.8.0", "qrcode": "^1.5.4" }, @@ -2655,6 +2656,12 @@ "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": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", diff --git a/SharpbinV3.Client/package.json b/SharpbinV3.Client/package.json index 81a0e83..3f6972d 100644 --- a/SharpbinV3.Client/package.json +++ b/SharpbinV3.Client/package.json @@ -39,6 +39,7 @@ "dependencies": { "@lucide/svelte": "^0.562.0", "@sveltejs/adapter-node": "^5.4.0", + "hash-wasm": "^4.12.0", "highlight.js": "^11.8.0", "qrcode": "^1.5.4" } diff --git a/SharpbinV3.Client/src/lib/utils/encryption.ts b/SharpbinV3.Client/src/lib/utils/encryption.ts index 1c653fd..34a1b4b 100644 --- a/SharpbinV3.Client/src/lib/utils/encryption.ts +++ b/SharpbinV3.Client/src/lib/utils/encryption.ts @@ -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 { 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', - 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 }, + keyMaterial as unknown as BufferSource, + { name: 'AES-GCM' }, false, ['encrypt'] ); + const iv = window.crypto.getRandomValues(new Uint8Array(12)); const encryptedContent = await window.crypto.subtle.encrypt( { name: 'AES-GCM', - iv: 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', + + return JSON.stringify({ + version: 4, + kdf: 'Argon2id', + memorySize, + iterations, + parallelism, algorithm: 'AES-GCM', - data: btoa(String.fromCharCode(...combined)) - }; - return JSON.stringify(result); + saltLength: salt.byteLength, + ivLength: iv.byteLength, + 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 { 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, + const combined = fromBase64(parsed.data); + + const saltLength = parsed.saltLength || 16; + const ivLength = parsed.ivLength || 12; + const salt = combined.slice(0, saltLength); + const iv = combined.slice(saltLength, saltLength + ivLength); + const data = combined.slice(saltLength + ivLength); + + let keyMaterial: ArrayBuffer | Uint8Array; + + if (parsed.kdf === 'Argon2id') { + keyMaterial = await argon2id({ + password, + salt, iterations: parsed.iterations, - hash: parsed.hash - }, - keyMaterial, - { name: 'AES-GCM', length: 256 }, + memorySize: parsed.memorySize, + parallelism: parsed.parallelism, + hashLength: 32, + 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, ['decrypt'] ); + const decryptedContent = await window.crypto.subtle.decrypt( { name: 'AES-GCM', - iv: iv + iv }, key, data ); - const dec = new TextDecoder(); - return dec.decode(decryptedContent); + + return new TextDecoder().decode(decryptedContent); } catch { return null; }