This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oRand.js
165 lines (137 loc) · 4.02 KB
/
oRand.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(function (thiz) {
var O = thiz.O;
// prng4.js - uses Arcfour as a PRNG
// Copyright (c) 2005 Tom Wu
// All Rights Reserved.
// See "LICENSE" for details.
O.__.PRNG4 = {
Arcfour: function () {
this.i = 0;
this.j = 0;
this.S = new Array();
// Initialize arcfour context from key, an array of ints, each from [0..255]
this.init = function (key) {
var i, j, t;
for(i = 0; i < 256; ++i)
this.S[i] = i;
j = 0;
for(i = 0; i < 256; ++i) {
j = (j + this.S[i] + key[i % key.length]) & 255;
t = this.S[i];
this.S[i] = this.S[j];
this.S[j] = t;
}
this.i = 0;
this.j = 0;
};
this.next = function () {
var t;
this.i = (this.i + 1) & 255;
this.j = (this.j + this.S[this.i]) & 255;
t = this.S[this.i];
this.S[this.i] = this.S[this.j];
this.S[this.j] = t;
return this.S[(t + this.S[this.i]) & 255];
};
}
,
// Plug in your RNG constructor here
prng_newstate: function () {
return new this.Arcfour();
}
,
// Pool size must be a multiple of 4 and greater than 32.
// An array of bytes the size of the pool will be passed to init()
rng_psize: 256
};
//Random number generator - requires a PRNG backend, e.g. prng4.js
//Copyright (c) 2005 Tom Wu
//All Rights Reserved.
//See "LICENSE" for details.
//For best results, put code like
//<body onClick='O.timeSeed();' onKeyPress='O.timeSeed();'>
//in your main HTML document.
O.__.RNG = {
rng_state: null,
rng_pool: [],
rng_pptr: 0,
// Mix in a 32-bit integer into the pool
rng_seed_int: function (x) {
var RNG = O.__.RNG;
RNG.rng_pool[RNG.rng_pptr++] ^= x & 255;
RNG.rng_pool[RNG.rng_pptr++] ^= (x >> 8) & 255;
RNG.rng_pool[RNG.rng_pptr++] ^= (x >> 16) & 255;
RNG.rng_pool[RNG.rng_pptr++] ^= (x >> 24) & 255;
if(RNG.rng_pptr >= O.__.PRNG4.rng_psize) RNG.rng_pptr -= O.__.PRNG4.rng_psize;
}
,
// Mix in the current time (w/milliseconds) into the pool
rng_seed_time: function () {
O.__.RNG.rng_seed_int(new Date().getTime());
}
,
// Initialize the pool with junk if needed.
pool_init: function () {
var t, RNG = O.__.RNG;
var nav = typeof navigator != 'undefined' ? navigator : {};
var win = typeof window != 'undefined' ? window : {};
while(RNG.rng_pptr < O.__.PRNG4.rng_psize) { // extract some randomness from Math.random()
t = Math.floor(65536 * Math.random());
RNG.rng_pool[RNG.rng_pptr++] = t >>> 8;
RNG.rng_pool[RNG.rng_pptr++] = t & 255;
}
RNG.rng_pptr = 0;
RNG.rng_seed_time();
RNG.rng_seed_int(win.screenX);
RNG.rng_seed_int(win.screenY);
}
,
rng_get_byte: function () {
var RNG = O.__.RNG;
if(RNG.rng_state == null) {
RNG.rng_seed_time();
RNG.rng_state = O.__.PRNG4.prng_newstate();
RNG.rng_state.init(RNG.rng_pool);
for(RNG.rng_pptr = 0; RNG.rng_pptr < RNG.rng_pool.length; ++RNG.rng_pptr)
RNG.rng_pool[RNG.rng_pptr] = 0;
RNG.rng_pptr = 0;
//RNG.rng_pool = null;
}
// TODO: allow reseeding after first request
return RNG.rng_state.next();
}
};
O.__.RNG.pool_init();
O.expand({
timeSeed: function() {
O.__.RNG.rng_seed_time();
},
SecureRandom: function () { // object used by RSA
this.nextBytes = function (ba) {
var i;
for(i = 0; i < ba.length; ++i) ba[i] = O.__.RNG.rng_get_byte();
};
},
getRandom: function (iterations) {
var ret = "",
n = iterations ? iterations : 1;
for (var j=0;j<n;j++) {
var rng = new O.SecureRandom(),
b = [],
x = [],
n;
b[16] = 1;
n = b.length;
while (n > 0) { // random non-zero pad
x[0] = 0;
while (x[0] == 0) rng.nextBytes(x);
b[--n] = x[0];
}
var r = b.join('').substring(1);
if (iterations) ret += r;
else return parseFloat("0."+r,10);
}
return ret;
}
});
})(this);