Skip to content

Commit 7e87fc7

Browse files
committed
add timingSafeEqual
1 parent eea13e5 commit 7e87fc7

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Here is the subset that is currently implemented:
2525
* createECDH (secp256k1)
2626
* publicEncrypt/privateDecrypt (rsa)
2727
* privateEncrypt/publicDecrypt (rsa)
28+
* timingSafeEqual
2829

2930
## todo
3031

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ var publicEncrypt = require('public-encrypt')
6363
exports[key] = publicEncrypt[key]
6464
})
6565

66+
exports.timingSafeEqual = require('timing-safe-equal')
67+
6668
// the least I can do is make error messages for the rest of the node.js/crypto api.
6769
;[
6870
'createCredentials'

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"inherits": "^2.0.1",
2828
"pbkdf2": "^3.0.3",
2929
"public-encrypt": "^4.0.0",
30-
"randombytes": "^2.0.0"
30+
"randombytes": "^2.0.0",
31+
"timing-safe-equal": "^1.0.0"
3132
},
3233
"devDependencies": {
3334
"hash-test-vectors": "~1.3.2",

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ try {
1212
require('./public-encrypt')
1313
require('./random-bytes')
1414
require('./sign')
15+
require('./timing-safe-equal')
1516
} catch (e) {
1617
console.log('no secure rng avaiable')
1718
}

test/timing-safe-equal.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var test = require('tape')
2+
var timingSafeEqual = require('timing-safe-equal/browser')
3+
test('timingSafeEqual', function (t) {
4+
t.plan(5)
5+
t.strictEqual(
6+
timingSafeEqual(Buffer.from('foo'), Buffer.from('foo')),
7+
true,
8+
'should consider equal strings to be equal'
9+
)
10+
11+
t.strictEqual(
12+
timingSafeEqual(Buffer.from('foo'), Buffer.from('bar')),
13+
false,
14+
'should consider unequal strings to be unequal'
15+
)
16+
17+
t.throws(function () {
18+
timingSafeEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2]))
19+
}, 'should throw when given buffers with different lengths')
20+
21+
t.throws(function () {
22+
timingSafeEqual('not a buffer', Buffer.from([1, 2]))
23+
}, 'should throw if the first argument is not a buffer')
24+
25+
t.throws(function () {
26+
timingSafeEqual(Buffer.from([1, 2]), 'not a buffer')
27+
}, 'should throw if the second argument is not a buffer')
28+
})

0 commit comments

Comments
 (0)