corrade-nucleus-nucleons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | 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 | if (!C.nextTick) { |
||
12 | // node.js has setTime out but prefer process.nextTick
|
||
13 | if (typeof process != 'undefined' && typeof process.nextTick !== 'undefined') { |
||
14 | C.nextTick = process.nextTick; |
||
15 | } else if (typeof setTimeout !== 'undefined') { |
||
16 | C.nextTick = function (callback) { |
||
17 | setTimeout(callback, 0); |
||
18 | }; |
||
19 | } |
||
20 | } |
||
21 | |||
22 | C.PBKDF2Async = function (password, salt, keylen, callback, options) { |
||
23 | |||
24 | // Convert to byte arrays
|
||
25 | if (password.constructor == String) password = UTF8.stringToBytes(password); |
||
26 | if (salt.constructor == String) salt = UTF8.stringToBytes(salt); |
||
27 | /* else, assume byte arrays already */ |
||
28 | |||
29 | // Defaults
|
||
30 | var hasher = options && options.hasher || C.SHA1, |
||
31 | iterations = options && options.iterations || 1; |
||
32 | |||
33 | // Progress callback option
|
||
34 | var progressChangeHandler = options && options.onProgressChange; |
||
35 | var totalIterations = Math.ceil(keylen / hasher._digestsize) * iterations;
|
||
36 | function fireProgressChange(currentIteration) {
|
||
37 | if (progressChangeHandler) {
|
||
38 | var iterationsSoFar = derivedKeyBytes.length / hasher._digestsize * iterations + currentIteration; |
||
39 | setTimeout(function () { |
||
40 | progressChangeHandler(Math.round(iterationsSoFar / totalIterations * 100));
|
||
41 | }, 0);
|
||
42 | }
|
||
43 | }
|
||
44 | |||
45 | // Pseudo-random function
|
||
46 | function PRF(password, salt) {
|
||
47 | return C.HMAC(hasher, salt, password, { asBytes: true });
|
||
48 | }
|
||
49 | |||
50 | var nextTick = C.nextTick;
|
||
51 | |||
52 | // Generate key
|
||
53 | var derivedKeyBytes = [],
|
||
54 | blockindex = 1;
|
||
55 | |||
56 | var outer, inner;
|
||
57 | nextTick(outer = function () {
|
||
58 | if (derivedKeyBytes.length < keylen) {
> |
||
59 | < keylen) {
var block = PRF(password, salt.concat(util.wordsToBytes([blockindex])));
> |
||
60 | < keylen) {
fireProgressChange(1);
> |
||
61 | |||
62 | < keylen) {
var u = block, i = 1;
> |
||
63 | < keylen) {
nextTick(inner = function () {
> |
||
64 | < keylen) {
if (i < iterations) {
>> |
||
65 | < keylen) {
< iterations) {
u = PRF(password, u);
>> |
||
66 | < keylen) {
< iterations) {
for (var j = 0; j < block.length; j++) block[j] ^= u[j];
>>> |
||
67 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
i++;
>>> |
||
68 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
fireProgressChange(i);
>>> |
||
69 | |||
70 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
nextTick(inner);
>>> |
||
71 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
} else {
>>> |
||
72 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
derivedKeyBytes = derivedKeyBytes.concat(block);
>>> |
||
73 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
blockindex++;
>>> |
||
74 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
nextTick(outer);
>>> |
||
75 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
}
>>> |
||
76 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
});
>>> |
||
77 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
} else {
>>> |
||
78 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
// Truncate excess bytes
>>> |
||
79 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
derivedKeyBytes.length = keylen;
>>> |
||
80 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
callback(
>>> |
||
81 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
options && options.asBytes ? derivedKeyBytes :
>>> |
||
82 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
options && options.asString ? Binary.bytesToString(derivedKeyBytes) :
>>> |
||
83 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
util.bytesToHex(derivedKeyBytes));
>>> |
||
84 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
}
>>> |
||
85 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
});
>>> |
||
86 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
};
>>> |
||
87 | |||
88 | < keylen) {
< iterations) {
< block.length; j++) block[j] ^= u[j];
})();
>>> |