mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
refactor(encryption): replace magic numbers with constants for improved readability and maintainability
This commit is contained in:
@@ -1,10 +1,21 @@
|
|||||||
import { argon2id } from 'hash-wasm';
|
import { argon2id } from 'hash-wasm';
|
||||||
|
|
||||||
|
const BASE64_CHUNK_SIZE = 0x8000;
|
||||||
|
const SALT_LENGTH = 16;
|
||||||
|
const IV_LENGTH = 12;
|
||||||
|
const ARGON2_MEMORY_SIZE = 262144;
|
||||||
|
const ARGON2_ITERATIONS = 3;
|
||||||
|
const ARGON2_PARALLELISM = 1;
|
||||||
|
const ARGON2_HASH_LENGTH = 32;
|
||||||
|
const AES_KEY_LENGTH = 256;
|
||||||
|
const ENCRYPTION_AAD = 'enc-v4';
|
||||||
|
const ENCRYPTED_DATA_OVERHEAD = 60;
|
||||||
|
const ENCRYPTED_METADATA_OVERHEAD = 150;
|
||||||
|
|
||||||
function toBase64(bytes: Uint8Array): string {
|
function toBase64(bytes: Uint8Array): string {
|
||||||
let binary = '';
|
let binary = '';
|
||||||
const chunkSize = 0x8000;
|
for (let i = 0; i < bytes.length; i += BASE64_CHUNK_SIZE) {
|
||||||
for (let i = 0; i < bytes.length; i += chunkSize) {
|
binary += String.fromCharCode(...bytes.subarray(i, i + BASE64_CHUNK_SIZE));
|
||||||
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
|
||||||
}
|
}
|
||||||
return btoa(binary);
|
return btoa(binary);
|
||||||
}
|
}
|
||||||
@@ -20,35 +31,32 @@ function fromBase64(data: string): Uint8Array {
|
|||||||
|
|
||||||
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 salt = window.crypto.getRandomValues(new Uint8Array(16));
|
const salt = window.crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
||||||
const memorySize = 262144;
|
|
||||||
const iterations = 3;
|
|
||||||
const parallelism = 1;
|
|
||||||
|
|
||||||
const keyMaterial = await argon2id({
|
const keyMaterial = await argon2id({
|
||||||
password,
|
password,
|
||||||
salt,
|
salt,
|
||||||
iterations,
|
iterations: ARGON2_ITERATIONS,
|
||||||
memorySize,
|
memorySize: ARGON2_MEMORY_SIZE,
|
||||||
parallelism,
|
parallelism: ARGON2_PARALLELISM,
|
||||||
hashLength: 32,
|
hashLength: ARGON2_HASH_LENGTH,
|
||||||
outputType: 'binary'
|
outputType: 'binary'
|
||||||
});
|
});
|
||||||
|
|
||||||
const key = await window.crypto.subtle.importKey(
|
const key = await window.crypto.subtle.importKey(
|
||||||
'raw',
|
'raw',
|
||||||
new Uint8Array(keyMaterial),
|
new Uint8Array(keyMaterial),
|
||||||
{ name: 'AES-GCM', length: 256 },
|
{ name: 'AES-GCM', length: AES_KEY_LENGTH },
|
||||||
false,
|
false,
|
||||||
['encrypt']
|
['encrypt']
|
||||||
);
|
);
|
||||||
|
|
||||||
const iv = window.crypto.getRandomValues(new Uint8Array(12));
|
const iv = window.crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
||||||
const encryptedContent = await window.crypto.subtle.encrypt(
|
const encryptedContent = await window.crypto.subtle.encrypt(
|
||||||
{
|
{
|
||||||
name: 'AES-GCM',
|
name: 'AES-GCM',
|
||||||
iv,
|
iv,
|
||||||
additionalData: enc.encode('enc-v4')
|
additionalData: enc.encode(ENCRYPTION_AAD)
|
||||||
},
|
},
|
||||||
key,
|
key,
|
||||||
enc.encode(content)
|
enc.encode(content)
|
||||||
@@ -62,9 +70,9 @@ export async function encryptAES(content: string, password: string): Promise<str
|
|||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
version: 4,
|
version: 4,
|
||||||
kdf: 'Argon2id',
|
kdf: 'Argon2id',
|
||||||
memorySize,
|
memorySize: ARGON2_MEMORY_SIZE,
|
||||||
iterations,
|
iterations: ARGON2_ITERATIONS,
|
||||||
parallelism,
|
parallelism: ARGON2_PARALLELISM,
|
||||||
algorithm: 'AES-GCM',
|
algorithm: 'AES-GCM',
|
||||||
saltLength: salt.byteLength,
|
saltLength: salt.byteLength,
|
||||||
ivLength: iv.byteLength,
|
ivLength: iv.byteLength,
|
||||||
@@ -74,9 +82,9 @@ export async function encryptAES(content: string, password: string): Promise<str
|
|||||||
|
|
||||||
export function estimateEncryptedSize(content: string): number {
|
export function estimateEncryptedSize(content: string): number {
|
||||||
const rawSize = new TextEncoder().encode(content).length;
|
const rawSize = new TextEncoder().encode(content).length;
|
||||||
const combinedSize = rawSize + 60;
|
const combinedSize = rawSize + ENCRYPTED_DATA_OVERHEAD;
|
||||||
const b64Size = Math.ceil(combinedSize / 3) * 4;
|
const b64Size = Math.ceil(combinedSize / 3) * 4;
|
||||||
return b64Size + 150;
|
return b64Size + ENCRYPTED_METADATA_OVERHEAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function decryptAES(result: string, password: string): Promise<string | null> {
|
export async function decryptAES(result: string, password: string): Promise<string | null> {
|
||||||
@@ -85,8 +93,8 @@ export async function decryptAES(result: string, password: string): Promise<stri
|
|||||||
const parsed = JSON.parse(result);
|
const parsed = JSON.parse(result);
|
||||||
const combined = fromBase64(parsed.data);
|
const combined = fromBase64(parsed.data);
|
||||||
|
|
||||||
const saltLength = parsed.saltLength || 16;
|
const saltLength = parsed.saltLength || SALT_LENGTH;
|
||||||
const ivLength = parsed.ivLength || 12;
|
const ivLength = parsed.ivLength || IV_LENGTH;
|
||||||
const salt = combined.slice(0, saltLength);
|
const salt = combined.slice(0, saltLength);
|
||||||
const iv = combined.slice(saltLength, saltLength + ivLength);
|
const iv = combined.slice(saltLength, saltLength + ivLength);
|
||||||
const data = combined.slice(saltLength + ivLength);
|
const data = combined.slice(saltLength + ivLength);
|
||||||
@@ -100,7 +108,7 @@ export async function decryptAES(result: string, password: string): Promise<stri
|
|||||||
iterations: parsed.iterations,
|
iterations: parsed.iterations,
|
||||||
memorySize: parsed.memorySize,
|
memorySize: parsed.memorySize,
|
||||||
parallelism: parsed.parallelism,
|
parallelism: parsed.parallelism,
|
||||||
hashLength: 32,
|
hashLength: ARGON2_HASH_LENGTH,
|
||||||
outputType: 'binary'
|
outputType: 'binary'
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -119,7 +127,7 @@ export async function decryptAES(result: string, password: string): Promise<stri
|
|||||||
hash: parsed.hash
|
hash: parsed.hash
|
||||||
},
|
},
|
||||||
baseKey,
|
baseKey,
|
||||||
{ name: 'AES-GCM', length: 256 },
|
{ name: 'AES-GCM', length: AES_KEY_LENGTH },
|
||||||
true,
|
true,
|
||||||
['encrypt', 'decrypt']
|
['encrypt', 'decrypt']
|
||||||
);
|
);
|
||||||
@@ -129,7 +137,7 @@ export async function decryptAES(result: string, password: string): Promise<stri
|
|||||||
const key = await window.crypto.subtle.importKey(
|
const key = await window.crypto.subtle.importKey(
|
||||||
'raw',
|
'raw',
|
||||||
new Uint8Array(keyMaterial),
|
new Uint8Array(keyMaterial),
|
||||||
{ name: 'AES-GCM', length: 256 },
|
{ name: 'AES-GCM', length: AES_KEY_LENGTH },
|
||||||
false,
|
false,
|
||||||
['decrypt']
|
['decrypt']
|
||||||
);
|
);
|
||||||
@@ -138,7 +146,7 @@ export async function decryptAES(result: string, password: string): Promise<stri
|
|||||||
{
|
{
|
||||||
name: 'AES-GCM',
|
name: 'AES-GCM',
|
||||||
iv,
|
iv,
|
||||||
additionalData: enc.encode('enc-v4')
|
additionalData: enc.encode(ENCRYPTION_AAD)
|
||||||
},
|
},
|
||||||
key,
|
key,
|
||||||
data
|
data
|
||||||
|
|||||||
Reference in New Issue
Block a user