index.js
1 /*jshint node:true */ 2 'use strict'; 3 var Buffer = require('buffer').Buffer; // browserify 4 var SlowBuffer = require('buffer').SlowBuffer; 5 6 module.exports = bufferEq; 7 8 function bufferEq(a, b) { 9 10 // shortcutting on type is necessary for correctness 11 if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { 12 return false; 13 } 14 15 // buffer sizes should be well-known information, so despite this 16 // shortcutting, it doesn't leak any information about the *contents* of the 17 // buffers. 18 if (a.length !== b.length) { 19 return false; 20 } 21 22 var c = 0; 23 for (var i = 0; i < a.length; i++) { 24 /*jshint bitwise:false */ 25 c |= a[i] ^ b[i]; // XOR 26 } 27 return c === 0; 28 } 29 30 bufferEq.install = function() { 31 Buffer.prototype.equal = SlowBuffer.prototype.equal = function equal(that) { 32 return bufferEq(this, that); 33 }; 34 }; 35 36 var origBufEqual = Buffer.prototype.equal; 37 var origSlowBufEqual = SlowBuffer.prototype.equal; 38 bufferEq.restore = function() { 39 Buffer.prototype.equal = origBufEqual; 40 SlowBuffer.prototype.equal = origSlowBufEqual; 41 };