root / trunk / web / dojo / dojox / math / random / Secure.js @ 12
History | View | Annotate | Download (1.27 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
4 | see: http://dojotoolkit.org/license for details
|
||
5 | */
|
||
6 | |||
7 | |||
8 | if(!dojo._hasResource["dojox.math.random.Secure"]){ |
||
9 | dojo._hasResource["dojox.math.random.Secure"]=true; |
||
10 | dojo.provide("dojox.math.random.Secure");
|
||
11 | dojo.declare("dojox.math.random.Secure",null,{constructor:function(_1,_2){ |
||
12 | this.prng=_1;
|
||
13 | var p=this.pool=new Array(_1.size); |
||
14 | this.pptr=0; |
||
15 | for(var i=0,_3=_1.size;i<_3;){ |
||
16 | var t=Math.floor(65536*Math.random()); |
||
17 | p[i++]=t>>>8;
|
||
18 | p[i++]=t&255;
|
||
19 | } |
||
20 | this.seedTime();
|
||
21 | if(!_2){
|
||
22 | this.h=[dojo.connect(dojo.body(),"onclick",this,"seedTime"),dojo.connect(dojo.body(),"onkeypress",this,"seedTime")]; |
||
23 | } |
||
24 | },destroy:function(){ |
||
25 | if(this.h){ |
||
26 | dojo.forEach(this.h,dojo.disconnect);
|
||
27 | } |
||
28 | },nextBytes:function(_4){ |
||
29 | var _5=this.state; |
||
30 | if(!_5){
|
||
31 | this.seedTime();
|
||
32 | _5=this.state=this.prng(); |
||
33 | _5.init(this.pool);
|
||
34 | for(var p=this.pool,i=0,_6=p.length;i<_6;p[i++]=0){ |
||
35 | } |
||
36 | this.pptr=0; |
||
37 | } |
||
38 | for(var i=0,_6=_4.length;i<_6;++i){ |
||
39 | _4[i]=_5.next(); |
||
40 | } |
||
41 | },seedTime:function(){ |
||
42 | this._seed_int(new Date().getTime()); |
||
43 | },_seed_int:function(x){ |
||
44 | var p=this.pool,i=this.pptr; |
||
45 | p[i++]^=x&255;
|
||
46 | p[i++]^=(x>>8)&255; |
||
47 | p[i++]^=(x>>16)&255; |
||
48 | p[i++]^=(x>>24)&255; |
||
49 | if(i>=this.prng.size){ |
||
50 | i-=this.prng.size;
|
||
51 | } |
||
52 | this.pptr=i;
|
||
53 | }}); |
||
54 | } |