react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / pbkdf2 / index.js
80540 viewsvar compat = require('./browser')1var crypto = require('crypto')2var fork = require('child_process').fork3var path = require('path')45var MAX_ALLOC = Math.pow(2, 30) - 1 // default in iojs67function asyncPBKDF2 (password, salt, iterations, keylen, digest, callback) {8if (typeof iterations !== 'number') {9throw new TypeError('Iterations not a number')10}1112if (iterations < 0) {13throw new TypeError('Bad iterations')14}1516if (typeof keylen !== 'number') {17throw new TypeError('Key length not a number')18}1920if (keylen < 0 || keylen > MAX_ALLOC) {21throw new TypeError('Bad key length')22}2324if (typeof password === 'string') {25password = new Buffer(password, 'binary')26}2728if (typeof salt === 'string') {29salt = new Buffer(salt, 'binary')30}3132var child = fork(path.resolve(__dirname, 'async-shim.js'))3334child.on('message', function (result) {35if (result.type === 'success') {36callback(null, new Buffer(result.data, 'hex'))37} else if (result.type === 'fail') {38callback(new TypeError(result.data))39}40})4142child.send({43password: password.toString('hex'),44salt: salt.toString('hex'),45iterations: iterations,46keylen: keylen,47digest: digest48})49}5051exports.pbkdf2Sync = function pbkdf2Sync (password, salt, iterations, keylen, digest) {52digest = digest || 'sha1'5354if (isNode10()) {55if (digest === 'sha1') {56return crypto.pbkdf2Sync(password, salt, iterations, keylen)57} else {58return compat.pbkdf2Sync(password, salt, iterations, keylen, digest)59}60} else {61return crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)62}63}6465exports.pbkdf2 = function pbkdf2 (password, salt, iterations, keylen, digest, callback) {66if (typeof digest === 'function') {67callback = digest68digest = 'sha1'69}7071if (isNode10()) {72if (digest === 'sha1') {73return crypto.pbkdf2(password, salt, iterations, keylen, callback)74} else {75return asyncPBKDF2(password, salt, iterations, keylen, digest, callback)76}77} else {78return crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)79}80}8182var sha1 = '0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164'83var isNode10Result8485function isNode10 () {86if (typeof isNode10Result === 'undefined') {87isNode10Result = crypto.pbkdf2Sync('password', 'salt', 1, 32, 'sha256').toString('hex') === sha188}8990return isNode10Result91}929394