summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/math/random
diff options
context:
space:
mode:
Diffstat (limited to 'js/dojo-1.7.2/dojox/math/random')
-rw-r--r--js/dojo-1.7.2/dojox/math/random/Secure.js99
-rw-r--r--js/dojo-1.7.2/dojox/math/random/Simple.js25
-rw-r--r--js/dojo-1.7.2/dojox/math/random/prng4.js60
3 files changed, 184 insertions, 0 deletions
diff --git a/js/dojo-1.7.2/dojox/math/random/Secure.js b/js/dojo-1.7.2/dojox/math/random/Secure.js
new file mode 100644
index 0000000..01a08bd
--- /dev/null
+++ b/js/dojo-1.7.2/dojox/math/random/Secure.js
@@ -0,0 +1,99 @@
+//>>built
+// AMD-ID "dojox/math/random/Secure"
+define("dojox/math/random/Secure", ["dojo"], function(dojo) {
+
+// Copyright (c) 2005 Tom Wu
+// All Rights Reserved.
+// See "LICENSE-BigInteger" for details.
+
+// Random number generator - requires a PRNG backend, e.g. prng4.js
+
+dojo.declare("dojox.math.random.Secure", null, {
+ // summary:
+ // Super simple implementation of a random number generator,
+ // which relies on Math.random().
+
+ constructor: function(prng, noEvents){
+ // summary:
+ // Intializes an instance of a secure random generator.
+ // prng: Function:
+ // function that returns an instance of PRNG (pseudorandom number generator)
+ // with two methods: init(array) and next(). It should have a property "size"
+ // to indicate the required pool size.
+ // noEvents: Boolean?:
+ // if false or absent, onclick and onkeypress event will be used to add
+ // "randomness", otherwise events will not be used.
+ this.prng = prng;
+
+ // Initialize the pool with junk if needed.
+ var p = this.pool = new Array(prng.size);
+ this.pptr = 0;
+ for(var i = 0, len = prng.size; i < len;) { // extract some randomness from Math.random()
+ var t = Math.floor(65536 * Math.random());
+ p[i++] = t >>> 8;
+ p[i++] = t & 255;
+ }
+ this.seedTime();
+
+ if(!noEvents){
+ this.h = [
+ dojo.connect(dojo.body(), "onclick", this, "seedTime"),
+ dojo.connect(dojo.body(), "onkeypress", this, "seedTime")
+ ];
+ }
+ },
+
+ destroy: function(){
+ // summary:
+ // Disconnects events, if any, preparing the object for GC.
+ if(this.h){
+ dojo.forEach(this.h, dojo.disconnect);
+ }
+ },
+
+ nextBytes: function(/* Array */ byteArray){
+ // summary:
+ // Fills in an array of bytes with random numbers
+ // byteArray: Array:
+ // array to be filled in with random numbers, only existing
+ // elements will be filled.
+
+ var state = this.state;
+
+ if(!state){
+ this.seedTime();
+ state = this.state = this.prng();
+ state.init(this.pool);
+ for(var p = this.pool, i = 0, len = p.length; i < len; p[i++] = 0);
+ this.pptr = 0;
+ //this.pool = null;
+ }
+
+ for(var i = 0, len = byteArray.length; i < len; ++i){
+ byteArray[i] = state.next();
+ }
+ },
+
+ seedTime: function() {
+ // summary:
+ // Mix in the current time (w/milliseconds) into the pool
+ this._seed_int(new Date().getTime());
+ },
+
+ _seed_int: function(x) {
+ // summary:
+ // Mix in a 32-bit integer into the pool
+ var p = this.pool, i = this.pptr;
+ p[i++] ^= x & 255;
+ p[i++] ^= (x >> 8) & 255;
+ p[i++] ^= (x >> 16) & 255;
+ p[i++] ^= (x >> 24) & 255;
+ if(i >= this.prng.size){
+ i -= this.prng.size;
+ }
+ this.pptr = i;
+ }
+});
+
+return dojox.math.random.Secure;
+});
diff --git a/js/dojo-1.7.2/dojox/math/random/Simple.js b/js/dojo-1.7.2/dojox/math/random/Simple.js
new file mode 100644
index 0000000..0061979
--- /dev/null
+++ b/js/dojo-1.7.2/dojox/math/random/Simple.js
@@ -0,0 +1,25 @@
+//>>built
+define("dojox/math/random/Simple", ["dojo"], function(dojo) {
+
+ return dojo.declare("dojox.math.random.Simple", null, {
+ // summary:
+ // Super simple implementation of a random number generator,
+ // which relies on Math.random().
+
+ destroy: function(){
+ // summary:
+ // Prepares the object for GC. (empty in this case)
+ },
+
+ nextBytes: function(/* Array */ byteArray){
+ // summary:
+ // Fills in an array of bytes with random numbers
+ // byteArray: Array:
+ // array to be filled in with random numbers, only existing
+ // elements will be filled.
+ for(var i = 0, l = byteArray.length; i < l; ++i){
+ byteArray[i] = Math.floor(256 * Math.random());
+ }
+ }
+ });
+});
diff --git a/js/dojo-1.7.2/dojox/math/random/prng4.js b/js/dojo-1.7.2/dojox/math/random/prng4.js
new file mode 100644
index 0000000..1d08b4d
--- /dev/null
+++ b/js/dojo-1.7.2/dojox/math/random/prng4.js
@@ -0,0 +1,60 @@
+//>>built
+// AMD-ID "dojox/math/random/prng4"
+define("dojox/math/random/prng4", ["dojo", "dojox"], function(dojo, dojox) {
+
+ dojo.getObject("math.random.prng4", true, dojox);
+
+// Copyright (c) 2005 Tom Wu
+// All Rights Reserved.
+// See "LICENSE-BigInteger" for details.
+
+ // prng4.js - uses Arcfour as a PRNG
+
+ function Arcfour() {
+ this.i = 0;
+ this.j = 0;
+ this.S = new Array(256);
+ }
+
+ dojo.extend(Arcfour, {
+ init: function(key){
+ // summary:
+ // Initialize arcfour context
+ // key: Array:
+ // an array of ints, each from [0..255]
+ var i, j, t, S = this.S, len = key.length;
+ for(i = 0; i < 256; ++i){
+ S[i] = i;
+ }
+ j = 0;
+ for(i = 0; i < 256; ++i){
+ j = (j + S[i] + key[i % len]) & 255;
+ t = S[i];
+ S[i] = S[j];
+ S[j] = t;
+ }
+ this.i = 0;
+ this.j = 0;
+ },
+
+ next: function(){
+ var t, i, j, S = this.S;
+ this.i = i = (this.i + 1) & 255;
+ this.j = j = (this.j + S[i]) & 255;
+ t = S[i];
+ S[i] = S[j];
+ S[j] = t;
+ return S[(t + S[i]) & 255];
+ }
+ });
+
+ dojox.math.random.prng4 = function(){
+ return new 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()
+ dojox.math.random.prng4.size = 256;
+
+ return dojox.math.random.prng4;
+});