diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-24 15:10:37 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-24 15:11:17 +0200 |
commit | 7a3df06eb573d36142bd1a8e03c5ce8752d300b3 (patch) | |
tree | 70bfaea8884c374876f607774850a3a51c0cb381 /node_modules/pbkdf2 | |
parent | aca1143cb9eed16cf37f04e475e4257418dd18ac (diff) |
fix build issues and add typedoc
Diffstat (limited to 'node_modules/pbkdf2')
-rw-r--r-- | node_modules/pbkdf2/browser.js | 68 | ||||
-rw-r--r-- | node_modules/pbkdf2/index.js | 1 | ||||
-rw-r--r-- | node_modules/pbkdf2/lib/async.js | 98 | ||||
-rw-r--r-- | node_modules/pbkdf2/lib/default-encoding.js | 10 | ||||
-rw-r--r-- | node_modules/pbkdf2/lib/precondition.js (renamed from node_modules/pbkdf2/precondition.js) | 0 | ||||
-rw-r--r-- | node_modules/pbkdf2/lib/sync-browser.js | 102 | ||||
-rw-r--r-- | node_modules/pbkdf2/lib/sync.js | 53 | ||||
l--------- | node_modules/pbkdf2/node_modules/.bin/sha.js | 1 | ||||
-rw-r--r-- | node_modules/pbkdf2/package.json | 30 |
9 files changed, 289 insertions, 74 deletions
diff --git a/node_modules/pbkdf2/browser.js b/node_modules/pbkdf2/browser.js index e07b5ad5a..98e615a85 100644 --- a/node_modules/pbkdf2/browser.js +++ b/node_modules/pbkdf2/browser.js @@ -1,68 +1,4 @@ -var createHmac = require('create-hmac') -var checkParameters = require('./precondition') -exports.pbkdf2 = function (password, salt, iterations, keylen, digest, callback) { - if (typeof digest === 'function') { - callback = digest - digest = undefined - } +exports.pbkdf2 = require('./lib/async') - checkParameters(iterations, keylen) - if (typeof callback !== 'function') throw new Error('No callback provided to pbkdf2') - - setTimeout(function () { - callback(null, exports.pbkdf2Sync(password, salt, iterations, keylen, digest)) - }) -} - -var defaultEncoding -if (process.browser) { - defaultEncoding = 'utf-8' -} else { - var pVersionMajor = parseInt(process.version.split('.')[0].slice(1), 10) - - defaultEncoding = pVersionMajor >= 6 ? 'utf-8' : 'binary' -} - -exports.pbkdf2Sync = function (password, salt, iterations, keylen, digest) { - if (!Buffer.isBuffer(password)) password = new Buffer(password, defaultEncoding) - if (!Buffer.isBuffer(salt)) salt = new Buffer(salt, defaultEncoding) - - checkParameters(iterations, keylen) - - digest = digest || 'sha1' - - var hLen - var l = 1 - var DK = new Buffer(keylen) - var block1 = new Buffer(salt.length + 4) - salt.copy(block1, 0, 0, salt.length) - - var r - var T - - for (var i = 1; i <= l; i++) { - block1.writeUInt32BE(i, salt.length) - var U = createHmac(digest, password).update(block1).digest() - - if (!hLen) { - hLen = U.length - T = new Buffer(hLen) - l = Math.ceil(keylen / hLen) - r = keylen - (l - 1) * hLen - } - - U.copy(T, 0, 0, hLen) - - for (var j = 1; j < iterations; j++) { - U = createHmac(digest, password).update(U).digest() - for (var k = 0; k < hLen; k++) T[k] ^= U[k] - } - - var destPos = (i - 1) * hLen - var len = (i === l ? r : hLen) - T.copy(DK, destPos, 0, len) - } - - return DK -} +exports.pbkdf2Sync = require('./lib/sync') diff --git a/node_modules/pbkdf2/index.js b/node_modules/pbkdf2/index.js index 2dc6c39fb..c3e4f5cb3 100644 --- a/node_modules/pbkdf2/index.js +++ b/node_modules/pbkdf2/index.js @@ -1,4 +1,5 @@ var crypto = require('crypto') +/* istanbul ignore next */ if (crypto.pbkdf2Sync.toString().indexOf('keylen, digest') === -1) { throw new Error('Unsupported crypto version') } diff --git a/node_modules/pbkdf2/lib/async.js b/node_modules/pbkdf2/lib/async.js new file mode 100644 index 000000000..c0b91b9a8 --- /dev/null +++ b/node_modules/pbkdf2/lib/async.js @@ -0,0 +1,98 @@ +var checkParameters = require('./precondition') +var defaultEncoding = require('./default-encoding') +var sync = require('./sync') +var Buffer = require('safe-buffer').Buffer + +var ZERO_BUF +var subtle = global.crypto && global.crypto.subtle +var toBrowser = { + 'sha': 'SHA-1', + 'sha-1': 'SHA-1', + 'sha1': 'SHA-1', + 'sha256': 'SHA-256', + 'sha-256': 'SHA-256', + 'sha384': 'SHA-384', + 'sha-384': 'SHA-384', + 'sha-512': 'SHA-512', + 'sha512': 'SHA-512' +} +var checks = [] +function checkNative (algo) { + if (global.process && !global.process.browser) { + return Promise.resolve(false) + } + if (!subtle || !subtle.importKey || !subtle.deriveBits) { + return Promise.resolve(false) + } + if (checks[algo] !== undefined) { + return checks[algo] + } + ZERO_BUF = ZERO_BUF || Buffer.alloc(8) + var prom = browserPbkdf2(ZERO_BUF, ZERO_BUF, 10, 128, algo) + .then(function () { + return true + }).catch(function () { + return false + }) + checks[algo] = prom + return prom +} +function browserPbkdf2 (password, salt, iterations, length, algo) { + return subtle.importKey( + 'raw', password, {name: 'PBKDF2'}, false, ['deriveBits'] + ).then(function (key) { + return subtle.deriveBits({ + name: 'PBKDF2', + salt: salt, + iterations: iterations, + hash: { + name: algo + } + }, key, length << 3) + }).then(function (res) { + return Buffer.from(res) + }) +} +function resolvePromise (promise, callback) { + promise.then(function (out) { + process.nextTick(function () { + callback(null, out) + }) + }, function (e) { + process.nextTick(function () { + callback(e) + }) + }) +} +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') { + callback = digest + digest = undefined + } + 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') { + return process.nextTick(function () { + var out + try { + out = sync(password, salt, iterations, keylen, digest) + } catch (e) { + return callback(e) + } + callback(null, out) + }) + } + resolvePromise(checkNative(algo).then(function (resp) { + if (resp) { + return browserPbkdf2(password, salt, iterations, keylen, algo) + } else { + return sync(password, salt, iterations, keylen, digest) + } + }), callback) +} diff --git a/node_modules/pbkdf2/lib/default-encoding.js b/node_modules/pbkdf2/lib/default-encoding.js new file mode 100644 index 000000000..13b8db92f --- /dev/null +++ b/node_modules/pbkdf2/lib/default-encoding.js @@ -0,0 +1,10 @@ +var defaultEncoding +/* istanbul ignore next */ +if (process.browser) { + defaultEncoding = 'utf-8' +} else { + var pVersionMajor = parseInt(process.version.split('.')[0].slice(1), 10) + + defaultEncoding = pVersionMajor >= 6 ? 'utf-8' : 'binary' +} +module.exports = defaultEncoding diff --git a/node_modules/pbkdf2/precondition.js b/node_modules/pbkdf2/lib/precondition.js index 1519b0092..1519b0092 100644 --- a/node_modules/pbkdf2/precondition.js +++ b/node_modules/pbkdf2/lib/precondition.js diff --git a/node_modules/pbkdf2/lib/sync-browser.js b/node_modules/pbkdf2/lib/sync-browser.js new file mode 100644 index 000000000..2452e9fe3 --- /dev/null +++ b/node_modules/pbkdf2/lib/sync-browser.js @@ -0,0 +1,102 @@ +var md5 = require('create-hash/md5') +var rmd160 = require('ripemd160') +var sha = require('sha.js') + +var checkParameters = require('./precondition') +var defaultEncoding = require('./default-encoding') +var Buffer = require('safe-buffer').Buffer +var ZEROS = Buffer.alloc(128) +var sizes = { + md5: 16, + sha1: 20, + sha224: 28, + sha256: 32, + sha384: 48, + sha512: 64, + rmd160: 20, + ripemd160: 20 +} +function Hmac (alg, key, saltLen) { + var hash = getDigest(alg) + var blocksize = (alg === 'sha512' || alg === 'sha384') ? 128 : 64 + + if (key.length > blocksize) { + key = hash(key) + } else if (key.length < blocksize) { + key = Buffer.concat([key, ZEROS], blocksize) + } + + var ipad = Buffer.allocUnsafe(blocksize + sizes[alg]) + var opad = Buffer.allocUnsafe(blocksize + sizes[alg]) + for (var i = 0; i < blocksize; i++) { + ipad[i] = key[i] ^ 0x36 + opad[i] = key[i] ^ 0x5C + } + + var ipad1 = Buffer.allocUnsafe(blocksize + saltLen + 4) + ipad.copy(ipad1, 0, 0, blocksize) + this.ipad1 = ipad1 + this.ipad2 = ipad + this.opad = opad + this.alg = alg + this.blocksize = blocksize + this.hash = hash + this.size = sizes[alg] +} + +Hmac.prototype.run = function (data, ipad) { + data.copy(ipad, this.blocksize) + var h = this.hash(ipad) + h.copy(this.opad, this.blocksize) + return this.hash(this.opad) +} + +function getDigest (alg) { + if (alg === 'rmd160' || alg === 'ripemd160') return rmd160 + if (alg === 'md5') return md5 + return shaFunc + + function shaFunc (data) { + return sha(alg).update(data).digest() + } +} + +module.exports = function (password, salt, iterations, keylen, digest) { + if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding) + if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding) + + checkParameters(iterations, keylen) + + digest = digest || 'sha1' + + var hmac = new Hmac(digest, password, salt.length) + + var DK = Buffer.allocUnsafe(keylen) + var block1 = Buffer.allocUnsafe(salt.length + 4) + salt.copy(block1, 0, 0, salt.length) + + var U, j, destPos, len + + var hLen = hmac.size + var T = Buffer.allocUnsafe(hLen) + var l = Math.ceil(keylen / hLen) + var r = keylen - (l - 1) * hLen + + for (var i = 1; i <= l; i++) { + block1.writeUInt32BE(i, salt.length) + U = hmac.run(block1, hmac.ipad1) + + U.copy(T, 0, 0, hLen) + + for (j = 1; j < iterations; j++) { + U = hmac.run(U, hmac.ipad2) + for (var k = 0; k < hLen; k++) T[k] ^= U[k] + } + + destPos = (i - 1) * hLen + len = (i === l ? r : hLen) + T.copy(DK, destPos, 0, len) + } + + return DK +} diff --git a/node_modules/pbkdf2/lib/sync.js b/node_modules/pbkdf2/lib/sync.js new file mode 100644 index 000000000..762e2f4e4 --- /dev/null +++ b/node_modules/pbkdf2/lib/sync.js @@ -0,0 +1,53 @@ +var sizes = { + md5: 16, + sha1: 20, + sha224: 28, + sha256: 32, + sha384: 48, + sha512: 64, + rmd160: 20, + ripemd160: 20 +} + +var createHmac = require('create-hmac') +var checkParameters = require('../lib/precondition') +var defaultEncoding = require('../lib/default-encoding') +var Buffer = require('safe-buffer').Buffer +module.exports = pbkdf2 +function pbkdf2 (password, salt, iterations, keylen, digest) { + if (!Buffer.isBuffer(password)) password = Buffer.from(password, defaultEncoding) + if (!Buffer.isBuffer(salt)) salt = Buffer.from(salt, defaultEncoding) + + checkParameters(iterations, keylen) + + digest = digest || 'sha1' + + var DK = Buffer.allocUnsafe(keylen) + var block1 = Buffer.allocUnsafe(salt.length + 4) + salt.copy(block1, 0, 0, salt.length) + + var U, j, destPos, len + + var hLen = sizes[digest] + var T = Buffer.allocUnsafe(hLen) + var l = Math.ceil(keylen / hLen) + var r = keylen - (l - 1) * hLen + + for (var i = 1; i <= l; i++) { + block1.writeUInt32BE(i, salt.length) + U = createHmac(digest, password).update(block1).digest() + + U.copy(T, 0, 0, hLen) + + for (j = 1; j < iterations; j++) { + U = createHmac(digest, password).update(U).digest() + for (var k = 0; k < hLen; k++) T[k] ^= U[k] + } + + destPos = (i - 1) * hLen + len = (i === l ? r : hLen) + T.copy(DK, destPos, 0, len) + } + + return DK +} diff --git a/node_modules/pbkdf2/node_modules/.bin/sha.js b/node_modules/pbkdf2/node_modules/.bin/sha.js new file mode 120000 index 000000000..4d6df2e5f --- /dev/null +++ b/node_modules/pbkdf2/node_modules/.bin/sha.js @@ -0,0 +1 @@ +../../../sha.js/bin.js
\ No newline at end of file diff --git a/node_modules/pbkdf2/package.json b/node_modules/pbkdf2/package.json index 9d4f40abe..fbd101898 100644 --- a/node_modules/pbkdf2/package.json +++ b/node_modules/pbkdf2/package.json @@ -1,6 +1,6 @@ { "name": "pbkdf2", - "version": "3.0.9", + "version": "3.0.12", "description": "This library provides the functionality of PBKDF2 with the ability to use any supported hashing algorithm returned from crypto.getHashes()", "keywords": [ "pbkdf2", @@ -14,13 +14,14 @@ }, "license": "MIT", "author": "Daniel Cousens", - "browser": "browser.js", + "browser": { + "./index.js": "./browser.js", + "./lib/sync.js": "./lib/sync-browser.js" + }, "files": [ "browser.js", "index.js", - "node-shim-async.js", - "node-shim.js", - "precondition.js" + "lib/" ], "main": "index.js", "repository": { @@ -33,16 +34,23 @@ "lint": "standard", "test": "npm run lint && npm run unit", "bundle-test": "browserify test/index.js > test/bundle.js", - "unit": "tape test/*.js" + "unit": "tape test/*.js", + "bench": "node bench/" }, "devDependencies": { + "benchmark": "^2.1.4", "browserify": "*", "nyc": "^6.4.0", "standard": "*", - "tape": "^4.5.1" + "tape": "^4.5.1", + "microtime": "^2.1.3" }, "dependencies": { - "create-hmac": "^1.1.2" + "create-hash": "^1.1.2", + "create-hmac": "^1.1.4", + "ripemd160": "^2.0.1", + "safe-buffer": "^5.0.1", + "sha.js": "^2.4.8" }, "standard": { "ignore": [ @@ -51,5 +59,11 @@ }, "engines": { "node": ">=0.12" + }, + "nyc": { + "exclude": [ + "lib/async.js", + "test/bundle.js" + ] } } |