mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: integrate hash-wasm for enhanced AES encryption and decryption
This commit is contained in:
Generated
+7
@@ -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",
|
||||||
|
|||||||
@@ -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"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
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,
|
||||||
|
memorySize: parsed.memorySize,
|
||||||
|
parallelism: parsed.parallelism,
|
||||||
|
hashLength: 32,
|
||||||
|
outputType: 'binary'
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const baseKey = await window.crypto.subtle.importKey(
|
||||||
'raw',
|
'raw',
|
||||||
enc.encode(password),
|
enc.encode(password),
|
||||||
{ name: 'PBKDF2' },
|
{ name: 'PBKDF2' },
|
||||||
false,
|
false,
|
||||||
['deriveBits', 'deriveKey']
|
['deriveBits', 'deriveKey']
|
||||||
);
|
);
|
||||||
const key = await window.crypto.subtle.deriveKey(
|
const derivedKey = await window.crypto.subtle.deriveKey(
|
||||||
{
|
{
|
||||||
name: 'PBKDF2',
|
name: 'PBKDF2',
|
||||||
salt: salt,
|
salt,
|
||||||
iterations: parsed.iterations,
|
iterations: parsed.iterations,
|
||||||
hash: parsed.hash
|
hash: parsed.hash
|
||||||
},
|
},
|
||||||
keyMaterial,
|
baseKey,
|
||||||
{ name: 'AES-GCM', length: 256 },
|
{ 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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user