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

add timingSafeEqual #156

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Here is the subset that is currently implemented:
* createECDH (secp256k1)
* publicEncrypt/privateDecrypt (rsa)
* privateEncrypt/publicDecrypt (rsa)
* timingSafeEqual

## todo

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ exports.privateEncrypt = publicEncrypt.privateEncrypt
exports.publicDecrypt = publicEncrypt.publicDecrypt
exports.privateDecrypt = publicEncrypt.privateDecrypt

exports.timingSafeEqual = require('timing-safe-equal')

// the least I can do is make error messages for the rest of the node.js/crypto api.
// ;[
// 'createCredentials'
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
"pbkdf2": "^3.0.3",
"public-encrypt": "^4.0.0",
"randombytes": "^2.0.0",
"randomfill": "^1.0.3"
"randomfill": "^1.0.3",
"timing-safe-equal": "^1.0.0"
},
"devDependencies": {
"hash-test-vectors": "~1.3.2",
"pseudorandombytes": "^2.0.0",
"safe-buffer": "^5.1.1",
"safe-buffer": "^5.1.2",
"standard": "^5.0.2",
"tape": "~2.3.2",
"zuul": "^3.6.0"
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ try {
require('./random-bytes')
require('./sign')
require('./random-fill')
require('./timing-safe-equal')
} catch (e) {
console.log('no secure rng avaiable')
}
Expand Down
29 changes: 29 additions & 0 deletions test/timing-safe-equal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var test = require('tape')
var timingSafeEqual = require('timing-safe-equal/browser')
var Buffer = require('safe-buffer').Buffer
test('timingSafeEqual', function (t) {
t.plan(5)
t.strictEqual(
timingSafeEqual(Buffer.from('foo'), Buffer.from('foo')),
true,
'should consider equal strings to be equal'
)

t.strictEqual(
timingSafeEqual(Buffer.from('foo'), Buffer.from('bar')),
false,
'should consider unequal strings to be unequal'
)

t.throws(function () {
timingSafeEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2]))
}, /TypeError: Input buffers must have the same length/, 'should throw when given buffers with different lengths')

t.throws(function () {
timingSafeEqual('not a buffer', Buffer.from([1, 2]))
}, /TypeError: First argument must be a buffer/, 'should throw if the first argument is not a buffer')

t.throws(function () {
timingSafeEqual(Buffer.from([1, 2]), 'not a buffer')
}, /TypeError: Second argument must be a buffer/, 'should throw if the second argument is not a buffer')
})