Merge remote-tracking branch 'remotes/origin/master' into feature/ik-handshake

This commit is contained in:
Belma Gutlic 2019-12-27 14:06:05 +01:00
commit d050db41c7
3 changed files with 5 additions and 16 deletions

View File

@ -11,7 +11,7 @@ const maxPlaintextLength = 65519;
export function encryptStream(handshake: Handshake): ReturnEncryptionWrapper { export function encryptStream(handshake: Handshake): ReturnEncryptionWrapper {
return async function * (source) { return async function * (source) {
for await (const chunk of source) { for await (const chunk of source) {
const chunkBuffer = Buffer.from(chunk); const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length);
for (let i = 0; i < chunkBuffer.length; i += maxPlaintextLength) { for (let i = 0; i < chunkBuffer.length; i += maxPlaintextLength) {
let end = i + maxPlaintextLength; let end = i + maxPlaintextLength;
@ -31,7 +31,7 @@ export function encryptStream(handshake: Handshake): ReturnEncryptionWrapper {
export function decryptStream(handshake: Handshake): ReturnEncryptionWrapper { export function decryptStream(handshake: Handshake): ReturnEncryptionWrapper {
return async function * (source) { return async function * (source) {
for await (const chunk of source) { for await (const chunk of source) {
const chunkBuffer = Buffer.from(chunk); const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length);
for (let i = 0; i < chunkBuffer.length; i += maxPlaintextLength) { for (let i = 0; i < chunkBuffer.length; i += maxPlaintextLength) {
let end = i + maxPlaintextLength; let end = i + maxPlaintextLength;

View File

@ -4,6 +4,7 @@ import DuplexPair from 'it-pair/duplex';
import { Noise } from "../src"; import { Noise } from "../src";
import {createPeerIdsFromFixtures} from "./fixtures/peer"; import {createPeerIdsFromFixtures} from "./fixtures/peer";
import Wrap from "it-pb-rpc"; import Wrap from "it-pb-rpc";
import { random } from "bcrypto";
import {Handshake} from "../src/handshake"; import {Handshake} from "../src/handshake";
import { import {
createHandshakePayload, createHandshakePayload,
@ -14,7 +15,7 @@ import {
import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder"; import { decodeMessageBuffer, encodeMessageBuffer } from "../src/encoder";
import {XXHandshake} from "../src/handshakes/xx"; import {XXHandshake} from "../src/handshakes/xx";
import {Buffer} from "buffer"; import {Buffer} from "buffer";
import {getKeyPairFromPeerId, getRandomBuffer} from "./utils"; import {getKeyPairFromPeerId} from "./utils";
describe("Noise", () => { describe("Noise", () => {
let remotePeer, localPeer; let remotePeer, localPeer;
@ -113,7 +114,7 @@ describe("Noise", () => {
const wrappedInbound = Wrap(inbound.conn); const wrappedInbound = Wrap(inbound.conn);
const wrappedOutbound = Wrap(outbound.conn); const wrappedOutbound = Wrap(outbound.conn);
const largePlaintext = getRandomBuffer(100000); const largePlaintext = random.randomBytes(100000);
wrappedOutbound.writeLP(largePlaintext); wrappedOutbound.writeLP(largePlaintext);
const response = await wrappedInbound.readLP(); const response = await wrappedInbound.readLP();

View File

@ -12,15 +12,3 @@ export function getKeyPairFromPeerId(peerId: PeerId): KeyPair {
publicKey: peerId.marshalPubKey(), publicKey: peerId.marshalPubKey(),
} }
} }
export function getRandomBuffer(size: number) : bytes {
size = Math.max(1, size<<0);
const buf = Buffer.alloc(size);
let i = 0;
for (; i < size; ++i) {
buf[i] = (Math.random() * 0xFF) << 0;
}
return buf;
}