| 1 |
|
| 2 |
define(["dojo"], function(dojo) { |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
dojo.declare("dojox.math.random.Secure", null, { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
constructor: function(prng, noEvents){ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
this.prng = prng; |
| 26 |
|
| 27 |
|
| 28 |
var p = this.pool = new Array(prng.size); |
| 29 |
this.pptr = 0; |
| 30 |
for(var i = 0, len = prng.size; i < len;) { |
| 31 |
var t = Math.floor(65536 * Math.random()); |
| 32 |
p[i++] = t >>> 8; |
| 33 |
p[i++] = t & 255; |
| 34 |
} |
| 35 |
this.seedTime(); |
| 36 |
|
| 37 |
if(!noEvents){ |
| 38 |
this.h = [ |
| 39 |
dojo.connect(dojo.body(), "onclick", this, "seedTime"), |
| 40 |
dojo.connect(dojo.body(), "onkeypress", this, "seedTime") |
| 41 |
]; |
| 42 |
} |
| 43 |
}, |
| 44 |
|
| 45 |
destroy: function(){ |
| 46 |
|
| 47 |
|
| 48 |
if(this.h){ |
| 49 |
dojo.forEach(this.h, dojo.disconnect); |
| 50 |
} |
| 51 |
}, |
| 52 |
|
| 53 |
nextBytes: function( byteArray){ |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
var state = this.state; |
| 61 |
|
| 62 |
if(!state){ |
| 63 |
this.seedTime(); |
| 64 |
state = this.state = this.prng(); |
| 65 |
state.init(this.pool); |
| 66 |
for(var p = this.pool, i = 0, len = p.length; i < len; p[i++] = 0); |
| 67 |
this.pptr = 0; |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
for(var i = 0, len = byteArray.length; i < len; ++i){ |
| 72 |
byteArray[i] = state.next(); |
| 73 |
} |
| 74 |
}, |
| 75 |
|
| 76 |
seedTime: function() { |
| 77 |
|
| 78 |
|
| 79 |
this._seed_int(new Date().getTime()); |
| 80 |
}, |
| 81 |
|
| 82 |
_seed_int: function(x) { |
| 83 |
|
| 84 |
|
| 85 |
var p = this.pool, i = this.pptr; |
| 86 |
p[i++] ^= x & 255; |
| 87 |
p[i++] ^= (x >> 8) & 255; |
| 88 |
p[i++] ^= (x >> 16) & 255; |
| 89 |
p[i++] ^= (x >> 24) & 255; |
| 90 |
if(i >= this.prng.size){ |
| 91 |
i -= this.prng.size; |
| 92 |
} |
| 93 |
this.pptr = i; |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
return dojox.math.random.Secure; |
| 98 |
}); |