crypto: use subtle for browser pbkdf2.
This commit is contained in:
parent
6c61ad976d
commit
4245cd6bf1
@ -12,15 +12,20 @@ var assert = require('assert');
|
|||||||
var hashjs = require('hash.js');
|
var hashjs = require('hash.js');
|
||||||
var aes = require('./aes');
|
var aes = require('./aes');
|
||||||
var backend = exports;
|
var backend = exports;
|
||||||
var crypto, global;
|
var global, crypto, subtle;
|
||||||
|
|
||||||
if (typeof window !== 'undefined')
|
global = (function() {
|
||||||
global = window;
|
if (typeof window !== 'undefined')
|
||||||
else if (typeof self !== 'undefined')
|
return window;
|
||||||
global = self;
|
|
||||||
|
|
||||||
if (global)
|
if (typeof self !== 'undefined')
|
||||||
crypto = global.crypto || global.msCrypto;
|
return self;
|
||||||
|
|
||||||
|
throw new Error('No global found.');
|
||||||
|
})();
|
||||||
|
|
||||||
|
crypto = global.crypto || global.msCrypto;
|
||||||
|
subtle = crypto ? crypto.subtle : null;
|
||||||
|
|
||||||
backend.hash = function hash(alg, data) {
|
backend.hash = function hash(alg, data) {
|
||||||
return new Buffer(hashjs[alg]().update(data).digest());
|
return new Buffer(hashjs[alg]().update(data).digest());
|
||||||
@ -39,7 +44,30 @@ backend.hmac = function hmac(alg, data, salt) {
|
|||||||
|
|
||||||
backend.pbkdf2 = null;
|
backend.pbkdf2 = null;
|
||||||
|
|
||||||
backend.pbkdf2Async = null;
|
backend.pbkdf2Async = function pbkdf2Async(key, salt, iter, len, alg) {
|
||||||
|
var algo = { name: 'PBKDF2' };
|
||||||
|
var use = ['deriveBits'];
|
||||||
|
var length = len * 8;
|
||||||
|
var options, promise;
|
||||||
|
|
||||||
|
options = {
|
||||||
|
name: 'PBKDF2',
|
||||||
|
salt: salt,
|
||||||
|
iterations: iter,
|
||||||
|
hash: getHash(alg)
|
||||||
|
};
|
||||||
|
|
||||||
|
promise = subtle.importKey('raw', key, algo, false, use);
|
||||||
|
|
||||||
|
return promise.then(function(key) {
|
||||||
|
return subtle.deriveBits(options, key, length);
|
||||||
|
}).then(function(result) {
|
||||||
|
return new Buffer(result);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!subtle || !subtle.importKey || !subtle.deriveBits)
|
||||||
|
backend.pbkdf2Async = null;
|
||||||
|
|
||||||
backend.encipher = function encipher(data, key, iv) {
|
backend.encipher = function encipher(data, key, iv) {
|
||||||
return aes.cbc.encrypt(data, key, iv);
|
return aes.cbc.encrypt(data, key, iv);
|
||||||
@ -71,3 +99,18 @@ if (!crypto || !crypto.getRandomValues) {
|
|||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getHash(name) {
|
||||||
|
switch (name) {
|
||||||
|
case 'sha1':
|
||||||
|
return 'SHA-1';
|
||||||
|
case 'sha256':
|
||||||
|
return 'SHA-256';
|
||||||
|
case 'sha384':
|
||||||
|
return 'SHA-384';
|
||||||
|
case 'sha512':
|
||||||
|
return 'SHA-512';
|
||||||
|
default:
|
||||||
|
throw new Error('Unknown hash.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user