-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.js
109 lines (108 loc) · 3.75 KB
/
random.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* An API for getting cryptographically-secure random bytes. The bytes are
* generated using the Fortuna algorithm devised by Bruce Schneier and
* Niels Ferguson.
*
* Getting strong random bytes is not yet easy to do in javascript. The only
* truish random entropy that can be collected is from the mouse, keyboard, or
* from timing with respect to page loads, etc. This generator makes a poor
* attempt at providing random bytes when those sources haven't yet provided
* enough entropy to initially seed or to reseed the PRNG.
*
* @author Dave Longley
*
* Copyright (c) 2009-2014 Digital Bazaar, Inc.
*/
if (module.exports && module.exports.getBytes) {
return;
}
(function(jQuery) {
var prng_aes = {};
var _prng_aes_output = new Array(4);
var _prng_aes_buffer = require('./util').createBuffer();
prng_aes.formatKey = function(key) {
var tmp = require('./util').createBuffer(key);
key = new Array(4);
key[0] = tmp.getInt32();
key[1] = tmp.getInt32();
key[2] = tmp.getInt32();
key[3] = tmp.getInt32();
return require('./aes')._expandKey(key, false);
};
prng_aes.formatSeed = function(seed) {
var tmp = require('./util').createBuffer(seed);
seed = new Array(4);
seed[0] = tmp.getInt32();
seed[1] = tmp.getInt32();
seed[2] = tmp.getInt32();
seed[3] = tmp.getInt32();
return seed;
};
prng_aes.cipher = function(key, seed) {
require('./aes')._updateBlock(key, seed, _prng_aes_output, false);
_prng_aes_buffer.putInt32(_prng_aes_output[0]);
_prng_aes_buffer.putInt32(_prng_aes_output[1]);
_prng_aes_buffer.putInt32(_prng_aes_output[2]);
_prng_aes_buffer.putInt32(_prng_aes_output[3]);
return _prng_aes_buffer.getBytes();
};
prng_aes.increment = function(seed) {
++seed[3];
return seed;
};
prng_aes.md = require('./md').sha256;
function spawnPrng() {
var ctx = require('./prng').create(prng_aes);
ctx.getBytes = function(count, callback) {
return ctx.generate(count, callback);
};
ctx.getBytesSync = function(count) {
return ctx.generate(count);
};
return ctx;
}
var _ctx = spawnPrng();
var _nodejs = typeof process !== 'undefined' && process.versions && process.versions.node;
var getRandomValues = null;
if (typeof window !== 'undefined') {
var _crypto = window.crypto || window.msCrypto;
if (_crypto && _crypto.getRandomValues) {
getRandomValues = function(arr) {
return _crypto.getRandomValues(arr);
};
}
}
if (!_nodejs && !getRandomValues) {
if (typeof window === 'undefined' || window.document === undefined) {}
_ctx.collectInt(+new Date(), 32);
if (typeof navigator !== 'undefined') {
var _navBytes = '';
for (var key in navigator) {
try {
if (typeof navigator[key] == 'string') {
_navBytes += navigator[key];
}
} catch (e) {}
}
_ctx.collect(_navBytes);
_navBytes = null;
}
if (jQuery) {
jQuery().mousemove(function(e) {
_ctx.collectInt(e.clientX, 16);
_ctx.collectInt(e.clientY, 16);
});
jQuery().keypress(function(e) {
_ctx.collectInt(e.charCode, 8);
});
}
}
if (!require('./random')) {
module.exports = _ctx;
} else {
for (var key in _ctx) {
module.exports[key] = _ctx[key];
}
}
module.exports.createInstance = spawnPrng;
}(typeof jQuery !== 'undefined' ? jQuery : null));