pbkdf2
Advanced tools
Comparing version 3.0.14 to 3.0.16
@@ -1,4 +0,2 @@ | ||
exports.pbkdf2 = require('./lib/async') | ||
exports.pbkdf2Sync = require('./lib/sync') |
32
index.js
@@ -1,9 +0,31 @@ | ||
var crypto = require('crypto') | ||
var checkParameters = require('./lib/precondition') | ||
var native = require('crypto') | ||
function nativePBKDF2 (password, salt, iterations, keylen, digest, callback) { | ||
checkParameters(password, salt, iterations, keylen) | ||
if (typeof digest === 'function') { | ||
callback = digest | ||
digest = 'sha1' | ||
} | ||
if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2') | ||
return native.pbkdf2(password, salt, iterations, keylen, digest, callback) | ||
} | ||
function nativePBKDF2Sync (password, salt, iterations, keylen, digest) { | ||
checkParameters(password, salt, iterations, keylen) | ||
digest = digest || 'sha1' | ||
return native.pbkdf2Sync(password, salt, iterations, keylen, digest) | ||
} | ||
/* istanbul ignore next */ | ||
if (crypto && (!crypto.pbkdf2Sync || crypto.pbkdf2Sync.toString().indexOf('keylen, digest') === -1)) { | ||
if (!native.pbkdf2Sync || native.pbkdf2Sync.toString().indexOf('keylen, digest') === -1) { | ||
exports.pbkdf2Sync = require('./lib/sync') | ||
exports.pbkdf2 = require('./lib/async') | ||
exports.pbkdf2Sync = require('./lib/sync') | ||
// native | ||
} else { | ||
exports.pbkdf2Sync = crypto.pbkdf2Sync | ||
exports.pbkdf2 = crypto.pbkdf2 | ||
exports.pbkdf2Sync = nativePBKDF2Sync | ||
exports.pbkdf2 = nativePBKDF2 | ||
} |
@@ -40,2 +40,3 @@ var checkParameters = require('./precondition') | ||
} | ||
function browserPbkdf2 (password, salt, iterations, length, algo) { | ||
@@ -57,2 +58,3 @@ return subtle.importKey( | ||
} | ||
function resolvePromise (promise, callback) { | ||
@@ -70,6 +72,2 @@ promise.then(function (out) { | ||
module.exports = function (password, salt, iterations, keylen, digest, callback) { | ||
if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding) | ||
if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding) | ||
checkParameters(iterations, keylen) | ||
if (typeof digest === 'function') { | ||
@@ -79,6 +77,6 @@ callback = digest | ||
} | ||
if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2') | ||
digest = digest || 'sha1' | ||
var algo = toBrowser[digest.toLowerCase()] | ||
if (!algo || typeof global.Promise !== 'function') { | ||
@@ -95,9 +93,13 @@ return process.nextTick(function () { | ||
} | ||
checkParameters(password, salt, iterations, keylen) | ||
if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2') | ||
if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding) | ||
if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding) | ||
resolvePromise(checkNative(algo).then(function (resp) { | ||
if (resp) { | ||
return browserPbkdf2(password, salt, iterations, keylen, algo) | ||
} else { | ||
return sync(password, salt, iterations, keylen, digest) | ||
} | ||
if (resp) return browserPbkdf2(password, salt, iterations, keylen, algo) | ||
return sync(password, salt, iterations, keylen, digest) | ||
}), callback) | ||
} |
var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs | ||
module.exports = function (iterations, keylen) { | ||
function checkBuffer (buf, name) { | ||
if (typeof buf !== 'string' && !Buffer.isBuffer(buf)) { | ||
throw new TypeError(name + ' must be a buffer or string') | ||
} | ||
} | ||
module.exports = function (password, salt, iterations, keylen) { | ||
checkBuffer(password, 'Password') | ||
checkBuffer(salt, 'Salt') | ||
if (typeof iterations !== 'number') { | ||
@@ -4,0 +14,0 @@ throw new TypeError('Iterations not a number') |
@@ -66,7 +66,7 @@ var md5 = require('create-hash/md5') | ||
function pbkdf2 (password, salt, iterations, keylen, digest) { | ||
checkParameters(password, salt, iterations, keylen) | ||
if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding) | ||
if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding) | ||
checkParameters(iterations, keylen) | ||
digest = digest || 'sha1' | ||
@@ -73,0 +73,0 @@ |
@@ -18,6 +18,7 @@ var sizes = { | ||
function pbkdf2 (password, salt, iterations, keylen, digest) { | ||
checkParameters(password, salt, iterations, keylen) | ||
if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding) | ||
if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding) | ||
checkParameters(iterations, keylen) | ||
digest = digest || 'sha1' | ||
@@ -24,0 +25,0 @@ |
{ | ||
"name": "pbkdf2", | ||
"version": "3.0.14", | ||
"version": "3.0.16", | ||
"description": "This library provides the functionality of PBKDF2 with the ability to use any supported hashing algorithm returned from crypto.getHashes()", | ||
@@ -33,3 +33,5 @@ "keywords": [ | ||
"prepublish": "npm run test", | ||
"coverage": "nyc --check-coverage --branches 90 --functions 100 tape test/*.js", | ||
"coverage-report": "nyc report --reporter=lcov", | ||
"coverage-html": "nyc report --reporter=html", | ||
"coverage": "nyc --check-coverage --branches 95 --functions 95 tape test/*.js", | ||
"lint": "standard", | ||
@@ -36,0 +38,0 @@ "test": "npm run lint && npm run unit", |
@@ -23,3 +23,5 @@ # pbkdf2 | ||
For high performance, use the `async` variant (`pbkdf2.pbkdf2`), not `pbkdf2.pbkdf2Sync`, this variant has the oppurtunity to use `window.crypto.subtle` when browserified. | ||
## Credits | ||
@@ -26,0 +28,0 @@ |
12836
270
31