|
1 | | -// JavaScript PBKDF2 Implementation |
2 | | -// Based on http://git.io/qsv2zw |
3 | | -// Licensed under LGPL v3 |
4 | | -// Copyright (c) 2013 jduncanator |
| 1 | +var pbkdf2Export = require('pbkdf2-compat').__pbkdf2Export |
5 | 2 |
|
6 | | -var blocksize = 64 |
7 | | -var zeroBuffer = new Buffer(blocksize); zeroBuffer.fill(0) |
8 | | - |
9 | | -module.exports = function (createHmac, exports) { |
| 3 | +module.exports = function (crypto, exports) { |
10 | 4 | exports = exports || {} |
11 | 5 |
|
12 | | - exports.pbkdf2 = function(password, salt, iterations, keylen, cb) { |
13 | | - if('function' !== typeof cb) |
14 | | - throw new Error('No callback provided to pbkdf2'); |
15 | | - setTimeout(function () { |
16 | | - cb(null, exports.pbkdf2Sync(password, salt, iterations, keylen)) |
17 | | - }) |
18 | | - } |
19 | | - |
20 | | - exports.pbkdf2Sync = function(key, salt, iterations, keylen) { |
21 | | - if('number' !== typeof iterations) |
22 | | - throw new TypeError('Iterations not a number') |
23 | | - if(iterations < 0) |
24 | | - throw new TypeError('Bad iterations') |
25 | | - if('number' !== typeof keylen) |
26 | | - throw new TypeError('Key length not a number') |
27 | | - if(keylen < 0) |
28 | | - throw new TypeError('Bad key length') |
29 | | - |
30 | | - //stretch key to the correct length that hmac wants it, |
31 | | - //otherwise this will happen every time hmac is called |
32 | | - //twice per iteration. |
33 | | - var key = !Buffer.isBuffer(key) ? new Buffer(key) : key |
34 | | - |
35 | | - if(key.length > blocksize) { |
36 | | - key = createHash(alg).update(key).digest() |
37 | | - } else if(key.length < blocksize) { |
38 | | - key = Buffer.concat([key, zeroBuffer], blocksize) |
39 | | - } |
40 | | - |
41 | | - var HMAC; |
42 | | - var cplen, p = 0, i = 1, itmp = new Buffer(4), digtmp; |
43 | | - var out = new Buffer(keylen); |
44 | | - out.fill(0); |
45 | | - while(keylen) { |
46 | | - if(keylen > 20) |
47 | | - cplen = 20; |
48 | | - else |
49 | | - cplen = keylen; |
50 | | - |
51 | | - /* We are unlikely to ever use more than 256 blocks (5120 bits!) |
52 | | - * but just in case... |
53 | | - */ |
54 | | - itmp[0] = (i >> 24) & 0xff; |
55 | | - itmp[1] = (i >> 16) & 0xff; |
56 | | - itmp[2] = (i >> 8) & 0xff; |
57 | | - itmp[3] = i & 0xff; |
58 | | - |
59 | | - HMAC = createHmac('sha1', key); |
60 | | - HMAC.update(salt) |
61 | | - HMAC.update(itmp); |
62 | | - digtmp = HMAC.digest(); |
63 | | - digtmp.copy(out, p, 0, cplen); |
64 | | - |
65 | | - for(var j = 1; j < iterations; j++) { |
66 | | - HMAC = createHmac('sha1', key); |
67 | | - HMAC.update(digtmp); |
68 | | - digtmp = HMAC.digest(); |
69 | | - for(var k = 0; k < cplen; k++) { |
70 | | - out[k] ^= digtmp[k]; |
71 | | - } |
72 | | - } |
73 | | - keylen -= cplen; |
74 | | - i++; |
75 | | - p += cplen; |
76 | | - } |
| 6 | + var exported = pbkdf2Export(crypto) |
77 | 7 |
|
78 | | - return out; |
79 | | - } |
| 8 | + exports.pbkdf2 = exported.pbkdf2 |
| 9 | + exports.pbkdf2Sync = exported.pbkdf2Sync |
80 | 10 |
|
81 | 11 | return exports |
82 | 12 | } |
0 commit comments