diff --git a/index.js b/index.js index 35501cb..5d7c02b 100644 --- a/index.js +++ b/index.js @@ -155,7 +155,7 @@ module.exports = class NoiseSecretStream extends Duplex { this._message = b4a.allocUnsafe(this._len) } - data.copy(this._message, this._tmp, offset) + b4a.copy(data, this._message, this._tmp, offset) this._tmp += unprocessed if (end <= data.length) { @@ -208,7 +208,7 @@ module.exports = class NoiseSecretStream extends Duplex { const expectedId = streamId(this.handshakeHash, !this.isInitiator) const header = message.subarray(32) - if (!expectedId.equals(remoteId)) { + if (!b4a.equals(expectedId, remoteId)) { this.destroy(new Error('Invalid header received')) return } diff --git a/lib/handshake.js b/lib/handshake.js index 2be702b..0ccda3d 100644 --- a/lib/handshake.js +++ b/lib/handshake.js @@ -1,8 +1,9 @@ const sodium = require('sodium-universal') const curve = require('noise-curve-ed') const Noise = require('noise-handshake') +const b4a = require('b4a') -const EMPTY = Buffer.alloc(0) +const EMPTY = b4a.alloc(0) module.exports = class Handshake { constructor (isInitiator, keyPair, remotePublicKey, pattern) { @@ -14,8 +15,8 @@ module.exports = class Handshake { } static keyPair (seed) { - const publicKey = Buffer.alloc(32) - const secretKey = Buffer.alloc(64) + const publicKey = b4a.alloc(32) + const secretKey = b4a.alloc(64) if (seed) sodium.crypto_sign_seed_keypair(publicKey, secretKey, seed) else sodium.crypto_sign_keypair(publicKey, secretKey) return { publicKey, secretKey } @@ -37,7 +38,7 @@ module.exports = class Handshake { send () { try { const data = this.noise.send() - const wrap = Buffer.allocUnsafe(data.byteLength + 3) + const wrap = b4a.allocUnsafe(data.byteLength + 3) writeUint24le(data.byteLength, wrap) wrap.set(data, 3) @@ -55,10 +56,10 @@ module.exports = class Handshake { } _return (data) { - const tx = this.noise.complete ? toBuffer(this.noise.tx) : null - const rx = this.noise.complete ? toBuffer(this.noise.rx) : null - const hash = this.noise.complete ? toBuffer(this.noise.hash) : null - const remotePublicKey = this.noise.complete ? toBuffer(this.noise.rs) : null + const tx = this.noise.complete ? b4a.toBuffer(this.noise.tx) : null + const rx = this.noise.complete ? b4a.toBuffer(this.noise.rx) : null + const hash = this.noise.complete ? b4a.toBuffer(this.noise.hash) : null + const remotePublicKey = this.noise.complete ? b4a.toBuffer(this.noise.rs) : null return { data, @@ -75,7 +76,3 @@ function writeUint24le (n, buf) { buf[1] = (n >>> 8) & 255 buf[2] = (n >>> 16) & 255 } - -function toBuffer (uint) { - return Buffer.from(uint.buffer, uint.byteOffset, uint.byteLength) -}