Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
var assert = require('assert')
2
var compatNode = require('../')
3
var compatBrowser = require('../browser')
4
var fixtures = require('./fixtures')
5
6
// SHA-1 vectors generated by Node.js
7
// SHA-256/SHA-512 test vectors from:
8
// https://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors
9
// https://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors
10
function runTests(compat, name) {
11
describe(name, function () {
12
var algos = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512']
13
describe('pbkdf2-compat', function() {
14
it('defaults to sha1 and handles buffers', function(done) {
15
compat.pbkdf2(new Buffer('password'), new Buffer('salt'), 1, 32, function (err, result) {
16
assert.equal(result.toString('hex'), "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164")
17
done()
18
})
19
})
20
21
describe('pbkdf2', function() {
22
algos.forEach(function(algorithm) {
23
describe(algorithm, function() {
24
fixtures.valid.forEach(function(f) {
25
var key = f.key || new Buffer(f.keyHex, 'hex')
26
var salt = f.salt || new Buffer(f.saltHex, 'hex')
27
var expected = f.results[algorithm]
28
29
it('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected, function(done) {
30
compat.pbkdf2(key, salt, f.iterations, f.dkLen, algorithm, function(err, result) {
31
assert.equal(result.toString('hex'), expected)
32
33
done()
34
})
35
})
36
})
37
38
fixtures.invalid.forEach(function(f) {
39
it('should throw ' + f.exception, function() {
40
assert.throws(function() {
41
compat.pbkdf2(f.key, f.salt, f.iterations, f.dkLen, f.algo, function (){})
42
}, new RegExp(f.exception))
43
})
44
})
45
})
46
})
47
48
it('should throw if no callback is provided', function() {
49
assert.throws(function() {
50
compat.pbkdf2('password', 'salt', 1, 32, 'sha1')
51
}, /No callback provided to pbkdf2/)
52
})
53
})
54
55
describe('pbkdf2Sync', function() {
56
it('defaults to sha1', function() {
57
var result = compat.pbkdf2Sync('password', 'salt', 1, 32)
58
59
assert.equal(result.toString('hex'), "0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164")
60
})
61
62
algos.forEach(function(algorithm) {
63
describe(algorithm, function() {
64
fixtures.valid.forEach(function(f) {
65
var key = f.key || new Buffer(f.keyHex, 'hex')
66
var salt = f.salt || new Buffer(f.saltHex, 'hex')
67
var expected = f.results[algorithm]
68
69
it('encodes ' + key + '(' + f.salt + ') with ' + algorithm + ' to ' + expected, function() {
70
var result = compat.pbkdf2Sync(key, salt, f.iterations, f.dkLen, algorithm)
71
72
assert.equal(result.toString('hex'), expected)
73
})
74
})
75
76
fixtures.invalid.forEach(function(f) {
77
it('should throw ' + f.exception, function() {
78
assert.throws(function() {
79
compat.pbkdf2Sync(f.key, f.salt, f.iterations, f.dkLen, f.algo)
80
}, new RegExp(f.exception))
81
})
82
})
83
})
84
})
85
})
86
})
87
})
88
}
89
90
runTests(compatBrowser, 'JavaScript pbkdf2')
91
if (compatBrowser !== compatNode) {
92
runTests(compatNode, 'node pbkdf2')
93
}
94
95