Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: inlined xor inplace #61

Merged
merged 4 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const randombytes = require('randombytes')
const sha1 = require('simple-sha1')
const speedometer = require('speedometer')
const stream = require('readable-stream')
const xor = require('buffer-xor')
const RC4 = require('rc4')

const BITFIELD_GROW = 400000
Expand All @@ -30,6 +29,11 @@ const VC = Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
const CRYPTO_PROVIDE = Buffer.from([0x00, 0x00, 0x01, 0x02])
const CRYPTO_SELECT = Buffer.from([0x00, 0x00, 0x00, 0x02]) // always try to choose RC4 encryption instead of plaintext

function xor (a, b) {
for (let i = 0, len = a.length; i < len; i++) a[i] = a[i] ^ b[i]
DiegoRBaquero marked this conversation as resolved.
Show resolved Hide resolved
return b
DiegoRBaquero marked this conversation as resolved.
Show resolved Hide resolved
}

class Request {
constructor (piece, offset, length, callback) {
this.piece = piece
Expand Down Expand Up @@ -599,7 +603,7 @@ class Wire extends stream.Duplex {

_onPe3 (hashesXorBuffer) {
const hash3 = sha1.sync(Buffer.from(this._utfToHex('req3') + this._sharedSecret, 'hex'))
const sKeyHash = xor(hashesXorBuffer, Buffer.from(hash3, 'hex')).toString('hex')
const sKeyHash = xor(Buffer.from(hash3, 'hex'), hashesXorBuffer).toString('hex')
this.emit('pe3', sKeyHash)
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"dependencies": {
"bencode": "^2.0.1",
"bitfield": "^4.0.0",
"buffer-xor": "^2.0.2",
"debug": "^4.3.1",
"randombytes": "^2.1.0",
"rc4": "^0.1.5",
Expand Down