diff --git a/src/plugins/nodePolyfills.ts b/src/plugins/nodePolyfills.ts index 6b24454..baa777b 100644 --- a/src/plugins/nodePolyfills.ts +++ b/src/plugins/nodePolyfills.ts @@ -274,22 +274,10 @@ const utilModule: any = { types: { isDate: (x: any) => x instanceof Date, isRegExp: (x: any) => x instanceof RegExp, isError: (x: any) => x instanceof Error, isArray: Array.isArray, isFunction: (x: any) => typeof x === 'function' }, }; -// Crypto helpers using Web Crypto API -async function subtleDigest(algo: string, data: Uint8Array): Promise { - const alg = algo === 'md5' ? 'MD5' : algo.toUpperCase(); - const name = alg === 'MD5' ? 'MD5' : alg === 'SHA1' ? 'SHA-1' : alg === 'SHA256' ? 'SHA-256' : alg === 'SHA512' ? 'SHA-512' : alg; - const sub = (globalThis as any).crypto?.subtle; - if (sub) { - try { return new Uint8Array(await sub.digest(name, data)); } catch { /* fallthrough */ } - } - return simpleHash(algo, data); -} - function simpleHash(algo: string, data: Uint8Array): Uint8Array { let s = ''; for (let i = 0; i < data.length; i++) s += String.fromCharCode(data[i]); const algoKey = algo.toLowerCase(); - let h: number; if (algoKey === 'md5') { let a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476; const msg = new Uint8Array(data.length + 1); @@ -372,8 +360,6 @@ function aesEcbEncrypt(data: Uint8Array, key: Uint8Array): Uint8Array { } w[i*4] = w[(i-Nk)*4] ^ temp[0]; w[i*4+1] = w[(i-Nk)*4+1] ^ temp[1]; w[i*4+2] = w[(i-Nk)*4+2] ^ temp[2]; w[i*4+3] = w[(i-Nk)*4+3] ^ temp[3]; } - function subWord(r: Uint8Array) { for (let i = 0; i < 4; i++) r[i] = sbox[r[i]]; } - function rotWord(r: Uint8Array) { const t = r[0]; r[0] = r[1]; r[1] = r[2]; r[2] = r[3]; r[3] = t; } function addRoundKey(state: Uint8Array, round: number) { for (let i = 0; i < 16; i++) state[i] ^= w[round * 16 + i]; } function subBytes(state: Uint8Array) { for (let i = 0; i < 16; i++) state[i] = sbox[state[i]]; } function shiftRows(state: Uint8Array) { @@ -481,48 +467,6 @@ function aesEcbDecrypt(data: Uint8Array, key: Uint8Array): Uint8Array { return out; } -async function aesCbcEncrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Promise { - const sub = (globalThis as any).crypto?.subtle; - if (sub) { - const alg = { name: 'AES-CBC', iv }; - const k = await sub.importKey('raw', key, alg, false, ['encrypt']); - return new Uint8Array(await sub.encrypt(alg, k, data)); - } - const padded = data.length % 16 === 0 ? data : (() => { const p = 16 - (data.length % 16); const out = new Uint8Array(data.length + p); out.set(data); for (let i = 0; i < p; i++) out[data.length + i] = p; return out; })(); - const blocks = padded.length / 16; - const out = new Uint8Array(padded.length); - let prev = new Uint8Array(iv); - for (let b = 0; b < blocks; b++) { - const block = new Uint8Array(padded.subarray(b * 16, (b + 1) * 16)); - for (let i = 0; i < 16; i++) block[i] ^= prev[i]; - const encrypted = aesEcbEncrypt(block, key); - out.set(encrypted, b * 16); - prev = encrypted; - } - return out; -} - -async function aesCbcDecrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Promise { - const sub = (globalThis as any).crypto?.subtle; - if (sub) { - const alg = { name: 'AES-CBC', iv }; - const k = await sub.importKey('raw', key, alg, false, ['decrypt']); - return new Uint8Array(await sub.decrypt(alg, k, data)); - } - const blocks = data.length / 16; - const out = new Uint8Array(data.length); - let prev = new Uint8Array(iv); - for (let b = 0; b < blocks; b++) { - const block = new Uint8Array(data.subarray(b * 16, (b + 1) * 16)); - const decrypted = aesEcbDecrypt(block, key); - for (let i = 0; i < 16; i++) out[b * 16 + i] = decrypted[i] ^ prev[i]; - prev = block; - } - const padLen = out[out.length - 1]; - if (padLen > 0 && padLen <= 16) return out.subarray(0, out.length - padLen); - return out; -} - function rsaPublicEncrypt(data: Uint8Array, keyPem: string): Uint8Array { const pemHeader = '-----BEGIN PUBLIC KEY-----'; const pemFooter = '-----END PUBLIC KEY-----'; @@ -551,10 +495,10 @@ function rsaPublicEncrypt(data: Uint8Array, keyPem: string): Uint8Array { readLen(); if (der[offset] === 0x30) { readTag(0x30); - const algoOid = readTag(0x06); + readTag(0x06); offset++; readLen(); if (der[offset++] !== 0x03) throw new Error('Invalid DER bit string'); - const bitLen = readLen(); + readLen(); offset++; if (der[offset++] !== 0x00) throw new Error('Invalid DER padding'); if (der[offset++] !== 0x30) throw new Error('Invalid DER inner sequence'); @@ -659,9 +603,8 @@ const cryptoModule: any = { return v.toString(16); }); }, - createCipheriv: function(algo: string, key: any, iv: any) { + createCipheriv: function(algo: string, key: any, _iv: any) { const keyBuf = toBuffer(key); - const ivBuf = iv ? toBuffer(iv) : new Uint8Array(0); return { update: function(data: any) { const dataBuf = toBuffer(data); @@ -671,9 +614,8 @@ const cryptoModule: any = { final: function() { return new Uint8Array(0); }, }; }, - createDecipheriv: function(algo: string, key: any, iv: any) { + createDecipheriv: function(algo: string, key: any, _iv: any) { const keyBuf = toBuffer(key); - const ivBuf = iv ? toBuffer(iv) : new Uint8Array(0); return { update: function(data: any) { const dataBuf = toBuffer(data); diff --git a/src/plugins/pluginManager.ts b/src/plugins/pluginManager.ts index e14cfdc..b7126bf 100644 --- a/src/plugins/pluginManager.ts +++ b/src/plugins/pluginManager.ts @@ -34,8 +34,6 @@ function getPluginSourcePrefixes(): string[] { ]; } -const PLUGIN_SOURCE_PREFIXES = getPluginSourcePrefixes(); - interface LoadedPlugin { id: string; name: string;