corrade-nucleus-nucleons – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
20 | office | 1 | (function(){ |
2 | |||
3 | var C = (typeof window === 'undefined') ? require('./Crypto').Crypto : window.Crypto; |
||
4 | |||
5 | // Shortcuts
|
||
6 | var util = C.util, |
||
7 | charenc = C.charenc, |
||
8 | UTF8 = charenc.UTF8, |
||
9 | Binary = charenc.Binary; |
||
10 | |||
11 | C.PBKDF2 = function (password, salt, keylen, options) { |
||
12 | |||
13 | // Convert to byte arrays
|
||
14 | if (password.constructor == String) password = UTF8.stringToBytes(password); |
||
15 | if (salt.constructor == String) salt = UTF8.stringToBytes(salt); |
||
16 | /* else, assume byte arrays already */ |
||
17 | |||
18 | // Defaults
|
||
19 | var hasher = options && options.hasher || C.SHA1, |
||
20 | iterations = options && options.iterations || 1; |
||
21 | |||
22 | // Pseudo-random function
|
||
23 | function PRF(password, salt) { |
||
24 | return C.HMAC(hasher, salt, password, { asBytes: true }); |
||
25 | } |
||
26 | |||
27 | // Generate key
|
||
28 | var derivedKeyBytes = [], |
||
29 | blockindex = 1; |
||
30 | while (derivedKeyBytes.length < keylen) { |
||
31 | var block = PRF(password, salt.concat(util.wordsToBytes([blockindex]))); |
||
32 | for (var u = block, i = 1; i < iterations; i++) { |
||
33 | u = PRF(password, u); |
||
34 | for (var j = 0; j < block.length; j++) block[j] ^= u[j]; |
||
35 | } |
||
36 | derivedKeyBytes = derivedKeyBytes.concat(block); |
||
37 | blockindex++; |
||
38 | } |
||
39 | |||
40 | // Truncate excess bytes
|
||
41 | derivedKeyBytes.length = keylen; |
||
42 | |||
43 | return options && options.asBytes ? derivedKeyBytes : |
||
44 | options && options.asString ? Binary.bytesToString(derivedKeyBytes) : |
||
45 | util.bytesToHex(derivedKeyBytes); |
||
46 | |||
47 | }; |
||
48 | |||
49 | })(); |