Skip to content

Commit

Permalink
pbkdf2: fix for browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jun 28, 2017
1 parent be1ec1c commit 2a5a8cd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
25 changes: 10 additions & 15 deletions lib/crypto/pbkdf2-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

var digest = require('./digest');
var crypto = global.crypto || global.msCrypto || {};
var subtle = crypto.subtle && crypto.subtle.importKey ? crypto.subtle : {};
var subtle = crypto.subtle || {};

/**
* Perform key derivation using PBKDF2.
Expand Down Expand Up @@ -62,32 +62,27 @@ exports.derive = function derive(key, salt, iter, len, alg) {
* @returns {Promise}
*/

exports.deriveAsync = function deriveAsync(key, salt, iter, len, alg) {
exports.deriveAsync = async function deriveAsync(key, salt, iter, len, alg) {
var algo = { name: 'PBKDF2' };
var use = ['deriveBits'];
var name = getHash(alg);
var length = len * 8;
var options, promise;
var options, key, data;

if (!subtle.importKey || !subtle.deriveBits)
return exports.derive(key, salt, iter, len, alg);

options = {
name: 'PBKDF2',
salt: salt,
iterations: iter,
hash: name
hash: getHash(alg)
};

promise = subtle.importKey('raw', key, algo, false, use);
key = await subtle.importKey('raw', key, algo, false, use);
data = await subtle.deriveBits(options, key, len * 8);

return promise.then(function(key) {
return subtle.deriveBits(options, key, length);
}).then(function(result) {
return Buffer.from(result);
});
return Buffer.from(data);
};

if (!subtle.deriveBits)
exports.pbkdf2Async = exports.pbkdf2;

/*
* Helpers
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* @module crypto.pbkdf2
*/

var co = require('../utils/co');
var crypto = require('crypto');
var co = require('../utils/co');

/**
* Perform key derivation using PBKDF2.
Expand Down

0 comments on commit 2a5a8cd

Please sign in to comment.