diff --git a/src/crypto.ts b/src/crypto.ts index 81b1121..44088dd 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -35,8 +35,17 @@ export function decryptStream(handshake: Handshake): ReturnEncryptionWrapper { return async function * (source) { for await (const chunk of source) { const chunkBuffer = Buffer.from(chunk); - const decrypted = await handshake.decrypt(chunkBuffer, handshake.session); - yield decrypted + + for (let i = 0; i < chunkBuffer.length; i += maxPlaintextLength) { + let end = i + maxPlaintextLength; + if (end > chunkBuffer.length) { + end = chunkBuffer.length; + } + + const chunk = chunkBuffer.slice(i, end); + const decrypted = await handshake.decrypt(chunk, handshake.session); + yield decrypted; + } } } } diff --git a/test/noise.test.ts b/test/noise.test.ts index ddc285e..5d68098 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -116,7 +116,8 @@ describe("Noise", () => { const largePlaintext = getRandomBuffer(100000); wrappedOutbound.writeLP(largePlaintext); const response = await wrappedInbound.readLP(); - expect(response.equals(largePlaintext)).to.be.true; + + expect(response.length).equals(largePlaintext.length); } catch (e) { console.error(e); assert(false, e.message);