Skip to content

tests: remove binary assumption, fixes #34 #36

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

Merged
merged 2 commits into from
Sep 12, 2016
Merged
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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ language: node_js
before_install:
- npm install npm -g
node_js:
- "0.10"
- "0.11"
- "0.12"
- "io.js"
- "4"
Expand Down
7 changes: 5 additions & 2 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var createHmac = require('create-hmac')
var checkParameters = require('./precondition')
var pVersionMajor = parseInt(process.version.split('.')[0].slice(1), 10)

exports.pbkdf2 = function (password, salt, iterations, keylen, digest, callback) {
if (typeof digest === 'function') {
Expand All @@ -15,9 +16,11 @@ exports.pbkdf2 = function (password, salt, iterations, keylen, digest, callback)
})
}

var defaultEncoding = (process.browser || pVersionMajor >= 6) ? 'utf-8' : 'binary'

exports.pbkdf2Sync = function (password, salt, iterations, keylen, digest) {
if (!Buffer.isBuffer(password)) password = new Buffer(password, 'binary')
if (!Buffer.isBuffer(salt)) salt = new Buffer(salt, 'binary')
if (!Buffer.isBuffer(password)) password = new Buffer(password, defaultEncoding)
if (!Buffer.isBuffer(salt)) salt = new Buffer(salt, defaultEncoding)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gah, this sucks. any ideas @calvinmetcalf @fanatid @indutny ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the situation, wondering if you had any thoughts on impl.
Otherwise, I assume this PR is good to go now


checkParameters(iterations, keylen)

Expand Down
19 changes: 2 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
var crypto = require('crypto')
var shim = require('./node-shim')

exports.pbkdf2Sync = function pbkdf2Sync (password, salt, iterations, keylen, digest) {
digest = digest || 'sha1'

var pbkdf2Sync = shim.isShimRequired() ? shim.pbkdf2Sync : crypto.pbkdf2Sync
return pbkdf2Sync(password, salt, iterations, keylen, digest)
}

exports.pbkdf2 = function pbkdf2 (password, salt, iterations, keylen, digest, callback) {
if (typeof digest === 'function') {
callback = digest
digest = 'sha1'
}

var pbkdf2 = shim.isShimRequired() ? shim.pbkdf2 : crypto.pbkdf2
return pbkdf2(password, salt, iterations, keylen, digest, callback)
}
exports.pbkdf2Sync = crypto.pbkdf2Sync
exports.pbkdf2 = crypto.pbkdf2
15 changes: 0 additions & 15 deletions node-shim-async.js

This file was deleted.

54 changes: 0 additions & 54 deletions node-shim.js

This file was deleted.

14 changes: 1 addition & 13 deletions test/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
}
},
{
"description": "Unicode salt, no truncation due to hex",
"key": "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
"saltHex": "6d6e656d6f6e6963e383a1e383bce38388e383abe382abe38299e3838fe38299e382a6e38299e382a1e381afe3829ae381afe38299e3818fe38299e3829de38299e381a1e381a1e38299e58d81e4babae58d81e889b2",
"iterations": 2048,
Expand All @@ -116,19 +117,6 @@
"sha224": "d76474c525616ce2a527d23df8d6f6fcc4251cc3535cae4e955810a51ead1ec6acbe9c9619187ca5a3c4fd636de5b5fe58d031714731290bbc081dbf0fcb8fc1",
"sha384": "15010450f456769467e834db7fa93dd9d353e8bb733b63b0621090f96599ac3316908eb64ac9366094f0787cd4bfb2fea25be41dc271a19309710db6144f9b34"
}
},
{
"key": "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
"salt": "mnemonicメートルガバヴァぱばぐゞちぢ十人十色",
"iterations": 2048,
"dkLen": 64,
"results": {
"sha1": "d85d14adcb7bdb5d976160e504f520a98cf71aca4cd5fceadf37759743bd6e1d2ff78bdd4403552aef7658094384b341ede80fffd334182be076f9d988a0a40f",
"sha256": "b86b5b900c29ed2724359afd793e10ffc1eb0e7d6f624fc9c85b8ac1785d9a2f0575af52a2338e611f2e6cffdee544adfff6f3d4f43be2ba0e2bd7e917b38a14",
"sha512": "3a863fa00f2e97a83fa9b18805e0047a6282cbae0ff48438b33a14475771c52d05137daa12e364cb34d84547ac07568b801c5c7f8dd4baaeee18a67a5c6a3377",
"sha224": "95727793842437774ad9ae27b8154a6f37f208b75a03d3a4d4a2443422bb6bc85efcfa92aa4376926ea89a8f5a63118eecdb58c8ca28ab31007da79437e0a1ef",
"sha384": "1a7e02e8ba0e357269a55642024b85738b95238d6cdc49bc440204995aefeff499e22cba76d4c7e96b7d4a9596a70e744f53fa94f3547e7dc506fcaf16ceb4a2"
}
}
],
"invalid": [
Expand Down
41 changes: 39 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ if (pVersionMajor !== 4 || process.browser) {
'exception': 'Bad key length'
})
}

if (pVersionMajor >= 5 || process.browser) {
fixtures.invalid.push({
'key': 'password',
Expand All @@ -35,6 +36,40 @@ if (pVersionMajor >= 5 || process.browser) {
})
}

if (pVersionMajor >= 6 || process.browser) {
fixtures.valid.push({
'description': 'Unicode salt, no truncation',
'key': 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
'salt': 'mnemonicメートルガバヴァぱばぐゞちぢ十人十色',
'iterations': 2048,
'dkLen': 64,
'results': {
'sha1': '7e042a2f41ba17e2235fbc794e22a150816b0f54a1dfe113919fccb7a056066a109385e538f183c92bad896ae8b7d8e0f4cd66df359c77c8c7785cd1001c9a2c',
'sha256': '0b57118f2b6b079d9371c94da3a8315c3ada87a1e819b40c4c4e90b36ff2d3c8fd7555538b5119ac4d3da7844aa4259d92f9dd2188e78ac33c4b08d8e6b5964b',
'sha512': 'ba553eedefe76e67e2602dc20184c564010859faada929a090dd2c57aacb204ceefd15404ab50ef3e8dbeae5195aeae64b0def4d2eead1cdc728a33ced520ffd',
'sha224': 'd76474c525616ce2a527d23df8d6f6fcc4251cc3535cae4e955810a51ead1ec6acbe9c9619187ca5a3c4fd636de5b5fe58d031714731290bbc081dbf0fcb8fc1',
'sha384': '15010450f456769467e834db7fa93dd9d353e8bb733b63b0621090f96599ac3316908eb64ac9366094f0787cd4bfb2fea25be41dc271a19309710db6144f9b34'
}
})

// 'binary' behaviour, Node 6 defaulted to 'utf-8'
} else {
fixtures.valid.push({
'description': 'Unicode salt, suffers from truncation',
'key': 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
'salt': 'mnemonicメートルガバヴァぱばぐゞちぢ十人十色',
'iterations': 2048,
'dkLen': 64,
'results': {
'sha1': 'd85d14adcb7bdb5d976160e504f520a98cf71aca4cd5fceadf37759743bd6e1d2ff78bdd4403552aef7658094384b341ede80fffd334182be076f9d988a0a40f',
'sha256': 'b86b5b900c29ed2724359afd793e10ffc1eb0e7d6f624fc9c85b8ac1785d9a2f0575af52a2338e611f2e6cffdee544adfff6f3d4f43be2ba0e2bd7e917b38a14',
'sha512': '3a863fa00f2e97a83fa9b18805e0047a6282cbae0ff48438b33a14475771c52d05137daa12e364cb34d84547ac07568b801c5c7f8dd4baaeee18a67a5c6a3377',
'sha224': '95727793842437774ad9ae27b8154a6f37f208b75a03d3a4d4a2443422bb6bc85efcfa92aa4376926ea89a8f5a63118eecdb58c8ca28ab31007da79437e0a1ef',
'sha384': '1a7e02e8ba0e357269a55642024b85738b95238d6cdc49bc440204995aefeff499e22cba76d4c7e96b7d4a9596a70e744f53fa94f3547e7dc506fcaf16ceb4a2'
}
})
}

// SHA-1 vectors generated by Node.js
// SHA-256/SHA-512 test vectors from:
// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
Expand All @@ -58,8 +93,9 @@ function runTests (name, compat) {
var key = f.key || new Buffer(f.keyHex, 'hex')
var salt = f.salt || new Buffer(f.saltHex, 'hex')
var expected = f.results[algorithm]
var description = f.description || ('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected)

it('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected, function (done) {
it(description, function (done) {
compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function (err, result) {
assert.equal(err, null)
assert.equal(result.toString('hex'), expected)
Expand Down Expand Up @@ -98,8 +134,9 @@ function runTests (name, compat) {
var key = f.key || new Buffer(f.keyHex, 'hex')
var salt = f.salt || new Buffer(f.saltHex, 'hex')
var expected = f.results[algorithm]
var description = f.description || ('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected)

it('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected, function () {
it(description, function () {
var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm)

assert.equal(result.toString('hex'), expected)
Expand Down