|
1 |
| -/* global describe, it */ |
2 |
| - |
3 |
| -var assert = require('assert') |
| 1 | +// SHA-1 vectors generated by Node.js |
| 2 | +// SHA-256/SHA-512 test vectors from: |
| 3 | +// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors |
| 4 | +// https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors |
4 | 5 | var fixtures = require('./fixtures')
|
| 6 | +var tape = require('tape') |
5 | 7 |
|
6 | 8 | var pVersionMajor = parseInt(process.version.split('.')[0].slice(1), 10)
|
7 | 9 | if (pVersionMajor !== 4 || process.browser) {
|
@@ -70,91 +72,76 @@ if (pVersionMajor >= 6 || process.browser) {
|
70 | 72 | })
|
71 | 73 | }
|
72 | 74 |
|
73 |
| -// SHA-1 vectors generated by Node.js |
74 |
| -// SHA-256/SHA-512 test vectors from: |
75 |
| -// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors |
76 |
| -// https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors |
77 | 75 | function runTests (name, compat) {
|
78 |
| - describe(name, function () { |
79 |
| - var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512'] |
80 |
| - describe('pbkdf2-compat', function () { |
81 |
| - it('defaults to sha1 and handles buffers', function (done) { |
82 |
| - compat.pbkdf2(new Buffer('password'), new Buffer('salt'), 1, 32, function (err, result) { |
83 |
| - assert.equal(err, null) |
84 |
| - assert.equal(result.toString('hex'), '0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164') |
85 |
| - done() |
| 76 | + tape(name + ' defaults to sha1 and handles buffers', function (t) { |
| 77 | + t.plan(2) |
| 78 | + |
| 79 | + compat.pbkdf2(new Buffer('password'), new Buffer('salt'), 1, 32, function (err, result) { |
| 80 | + t.error(err) |
| 81 | + t.equal(result.toString('hex'), '0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164') |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + tape(name + ' should throw if no callback is provided', function (t) { |
| 86 | + t.plan(1) |
| 87 | + |
| 88 | + t.throws(function () { |
| 89 | + compat.pbkdf2('password', 'salt', 1, 32, 'sha1') |
| 90 | + }, /No callback provided to pbkdf2/) |
| 91 | + }) |
| 92 | + |
| 93 | + var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512'] |
| 94 | + algos.forEach(function (algorithm) { |
| 95 | + fixtures.valid.forEach(function (f) { |
| 96 | + var key = f.key || new Buffer(f.keyHex, 'hex') |
| 97 | + var salt = f.salt || new Buffer(f.saltHex, 'hex') |
| 98 | + var expected = f.results[algorithm] |
| 99 | + var description = algorithm + ' encodes ' + key + ' (' + f.salt + ') with ' + algorithm + ' to ' + expected |
| 100 | + |
| 101 | + tape(name + ' async w/ ' + description, function (t) { |
| 102 | + t.plan(2) |
| 103 | + |
| 104 | + compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function (err, result) { |
| 105 | + t.error(err) |
| 106 | + t.equal(result.toString('hex'), expected) |
86 | 107 | })
|
87 | 108 | })
|
88 | 109 |
|
89 |
| - describe('pbkdf2', function () { |
90 |
| - algos.forEach(function (algorithm) { |
91 |
| - describe(algorithm, function () { |
92 |
| - fixtures.valid.forEach(function (f) { |
93 |
| - var key = f.key || new Buffer(f.keyHex, 'hex') |
94 |
| - var salt = f.salt || new Buffer(f.saltHex, 'hex') |
95 |
| - var expected = f.results[algorithm] |
96 |
| - var description = f.description || ('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected) |
97 |
| - |
98 |
| - it(description, function (done) { |
99 |
| - compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function (err, result) { |
100 |
| - assert.equal(err, null) |
101 |
| - assert.equal(result.toString('hex'), expected) |
102 |
| - done() |
103 |
| - }) |
104 |
| - }) |
105 |
| - }) |
106 |
| - |
107 |
| - fixtures.invalid.forEach(function (f) { |
108 |
| - it('should throw ' + f.exception, function () { |
109 |
| - assert.throws(function () { |
110 |
| - compat.pbkdf2(f.key, f.salt, f.iterations, f.dkLen, f.algo, function () {}) |
111 |
| - }, new RegExp(f.exception)) |
112 |
| - }) |
113 |
| - }) |
114 |
| - }) |
115 |
| - }) |
| 110 | + tape(name + 'sync w/ ' + description, function (t) { |
| 111 | + t.plan(1) |
116 | 112 |
|
117 |
| - it('should throw if no callback is provided', function () { |
118 |
| - assert.throws(function () { |
119 |
| - compat.pbkdf2('password', 'salt', 1, 32, 'sha1') |
120 |
| - }, /No callback provided to pbkdf2/) |
121 |
| - }) |
| 113 | + var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm) |
| 114 | + t.equal(result.toString('hex'), expected) |
122 | 115 | })
|
| 116 | + }) |
123 | 117 |
|
124 |
| - describe('pbkdf2Sync', function () { |
125 |
| - it('defaults to sha1', function () { |
126 |
| - var result = compat.pbkdf2Sync('password', 'salt', 1, 32) |
| 118 | + fixtures.invalid.forEach(function (f) { |
| 119 | + var description = algorithm + ' should throw ' + f.exception |
127 | 120 |
|
128 |
| - assert.equal(result.toString('hex'), '0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164') |
129 |
| - }) |
| 121 | + tape(name + ' async w/ ' + description, function (t) { |
| 122 | + t.plan(1) |
130 | 123 |
|
131 |
| - algos.forEach(function (algorithm) { |
132 |
| - describe(algorithm, function () { |
133 |
| - fixtures.valid.forEach(function (f) { |
134 |
| - var key = f.key || new Buffer(f.keyHex, 'hex') |
135 |
| - var salt = f.salt || new Buffer(f.saltHex, 'hex') |
136 |
| - var expected = f.results[algorithm] |
137 |
| - var description = f.description || ('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected) |
138 |
| - |
139 |
| - it(description, function () { |
140 |
| - var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm) |
141 |
| - |
142 |
| - assert.equal(result.toString('hex'), expected) |
143 |
| - }) |
144 |
| - }) |
145 |
| - |
146 |
| - fixtures.invalid.forEach(function (f) { |
147 |
| - it('should throw ' + f.exception, function () { |
148 |
| - assert.throws(function () { |
149 |
| - compat.pbkdf2Sync(f.key, f.salt, f.iterations, f.dkLen, f.algo) |
150 |
| - }, new RegExp(f.exception)) |
151 |
| - }) |
152 |
| - }) |
153 |
| - }) |
154 |
| - }) |
| 124 | + t.throws(function () { |
| 125 | + compat.pbkdf2(f.key, f.salt, f.iterations, f.dkLen, f.algo, function () {}) |
| 126 | + }, new RegExp(f.exception)) |
| 127 | + }) |
| 128 | + |
| 129 | + tape(name + ' sync w/' + description, function (t) { |
| 130 | + t.plan(1) |
| 131 | + |
| 132 | + t.throws(function () { |
| 133 | + compat.pbkdf2Sync(f.key, f.salt, f.iterations, f.dkLen, f.algo) |
| 134 | + }, new RegExp(f.exception)) |
155 | 135 | })
|
156 | 136 | })
|
157 | 137 | })
|
| 138 | + |
| 139 | + tape('pbkdf2Sync defaults to sha1', function (t) { |
| 140 | + t.plan(1) |
| 141 | + |
| 142 | + var result = compat.pbkdf2Sync('password', 'salt', 1, 32) |
| 143 | + t.equal(result.toString('hex'), '0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164') |
| 144 | + }) |
158 | 145 | }
|
159 | 146 |
|
160 | 147 | runTests('JavaScript pbkdf2', require('../browser'))
|
|
0 commit comments