react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / pbkdf2 / test / index.js
80551 viewsvar assert = require('assert')1var compatNode = require('../')2var compatBrowser = require('../browser')3var fixtures = require('./fixtures')45// SHA-1 vectors generated by Node.js6// SHA-256/SHA-512 test vectors from:7// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors8// https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors9function runTests(compat, name) {10describe(name, function () {11var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512']12describe('pbkdf2-compat', function() {13it('defaults to sha1 and handles buffers', function(done) {14compat.pbkdf2(new Buffer('password'), new Buffer('salt'), 1, 32, function (err, result) {15assert.equal(result.toString('hex'), "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164")16done()17})18})1920describe('pbkdf2', function() {21algos.forEach(function(algorithm) {22describe(algorithm, function() {23fixtures.valid.forEach(function(f) {24var key = f.key || new Buffer(f.keyHex, 'hex')25var salt = f.salt || new Buffer(f.saltHex, 'hex')26var expected = f.results[algorithm]2728it('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected, function(done) {29compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function(err, result) {30assert.equal(result.toString('hex'), expected)3132done()33})34})35})3637fixtures.invalid.forEach(function(f) {38it('should throw ' + f.exception, function() {39assert.throws(function() {40compat.pbkdf2(f.key, f.salt, f.iterations, f.dkLen, f.algo, function (){})41}, new RegExp(f.exception))42})43})44})45})4647it('should throw if no callback is provided', function() {48assert.throws(function() {49compat.pbkdf2('password', 'salt', 1, 32, 'sha1')50}, /No callback provided to pbkdf2/)51})52})5354describe('pbkdf2Sync', function() {55it('defaults to sha1', function() {56var result = compat.pbkdf2Sync('password', 'salt', 1, 32)5758assert.equal(result.toString('hex'), "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164")59})6061algos.forEach(function(algorithm) {62describe(algorithm, function() {63fixtures.valid.forEach(function(f) {64var key = f.key || new Buffer(f.keyHex, 'hex')65var salt = f.salt || new Buffer(f.saltHex, 'hex')66var expected = f.results[algorithm]6768it('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected, function() {69var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm)7071assert.equal(result.toString('hex'), expected)72})73})7475fixtures.invalid.forEach(function(f) {76it('should throw ' + f.exception, function() {77assert.throws(function() {78compat.pbkdf2Sync(f.key, f.salt, f.iterations, f.dkLen, f.algo)79}, new RegExp(f.exception))80})81})82})83})84})85})86})87}8889runTests(compatBrowser, 'JavaScript pbkdf2')90if (compatBrowser !== compatNode) {91runTests(compatNode, 'node pbkdf2')92}939495