Skip to content

Commit 5840d64

Browse files
committed
timingSafeEqual: final === check and type checking
1 parent 1af1602 commit 5840d64

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

lib/crypto.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ Hmac.prototype._transform = Hash.prototype._transform;
111111
//
112112
// Note: hmac object can not be used after validate() method has been called.
113113
Hmac.prototype.validate = function(inputBuffer) {
114+
if (!(inputBuffer instanceof Buffer)) {
115+
throw new TypeError('Argument should be a Buffer');
116+
}
114117
var ah = new Hmac('sha256', this.key).update(this.digest()).digest();
115118
var bh = new Hmac('sha256', this.key).update(inputBuffer).digest();
116119
return ah.equals(bh);
@@ -687,7 +690,11 @@ function filterDuplicates(names) {
687690
exports.timingSafeEqual = function(a, b) {
688691
var key = randomBytes(32);
689692
var ah = new Hmac('sha256', key).update(a);
690-
return ah.validate(new Hmac('sha256', key).update(b).digest());
693+
// The final === test is just in case of the vanishingly small chance of
694+
// a collision. It only fires if the digest comparison passes and so doesn't
695+
// leak timing information.
696+
return ah.validate(new Hmac('sha256', key).update(b).digest()) &&
697+
a.toString() === b.toString();
691698
};
692699

693700
// Legacy API

0 commit comments

Comments
 (0)