diff --git a/scrypt.js b/scrypt.js index 406c654..0d01b78 100644 --- a/scrypt.js +++ b/scrypt.js @@ -251,7 +251,7 @@ } function ensureInteger(value, name) { - if (typeof(value) !== "number" || (value % 1)) { throw new Error('invalid ' + name); } + if (!Number.isInteger(value)) { throw new Error('invalid ' + name); } return value; } diff --git a/test/test-scrypt.js b/test/test-scrypt.js index 5baaedd..79a37fc 100644 --- a/test/test-scrypt.js +++ b/test/test-scrypt.js @@ -6,6 +6,20 @@ const scrypt = require('../scrypt.js'); const testVectors = require('./test-vectors.json'); +for (const invalid of [NaN, Infinity, -Infinity, 0.5, null]) { + for (let i = 0; i < 4; i++) { + const password = Buffer.from('password'); + const salt = Buffer.from('salt'); + const argname = ['N', 'r', 'p', 'dkLen'][i]; + const args = [16, 1, 1, 64]; + args[i] = invalid; + it(`Test ${invalid} rejection ${i}`, () => assert.rejects( + () => scrypt.scrypt(password, salt, ...args, () => {}), + { message: `invalid ${argname}` } + )); + } +} + for (let i = 0; i < testVectors.length; i++) { const test = testVectors[i];