From 19c4543e44c3f4f22e0ada1878d2b6da2772b7a7 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 15 Sep 2014 09:10:50 -0700 Subject: [PATCH] use git-sha1 (for smaller bundle size) --- index.js | 11 +++-------- package.json | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 7efd5cc..5bf2cac 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,13 @@ var bencode = require('bencode') var BitField = require('bitfield') -var bufferEqual = require('buffer-equal') -var crypto = require('crypto') var EventEmitter = require('events').EventEmitter var inherits = require('inherits') +var sha1 = require('git-sha1') var MAX_METADATA_SIZE = 10000000 // 10MB var BITFIELD_GROW = 1000 var PIECE_LENGTH = 16 * 1024 -// Return sha1 hash **as a buffer** -function sha1 (buf) { - return crypto.createHash('sha1').update(buf).digest() -} - module.exports = function (metadata) { inherits(ut_metadata, EventEmitter) @@ -43,6 +37,7 @@ module.exports = function (metadata) { ut_metadata.prototype.onHandshake = function (infoHash, peerId, extensions) { this._infoHash = infoHash + this._infoHashHex = infoHash.toString('hex') } ut_metadata.prototype.onExtendedHandshake = function (handshake) { @@ -131,7 +126,7 @@ module.exports = function (metadata) { } catch (err) {} // check hash - if (this._infoHash && !bufferEqual(this._infoHash, sha1(metadata))) { + if (this._infoHashHex && this._infoHashHex !== sha1(metadata)) { return false } diff --git a/package.json b/package.json index 4a10797..4b8c49c 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "dependencies": { "bencode": "^0.6.0", "bitfield": "^1.0.1", - "buffer-equal": "0.0.1", + "git-sha1": "^0.1.2", "inherits": "^2.0.1" }, "devDependencies": {