root / trunk / web / dojo / dojox / sql / _crypto.js @ 9
History | View | Annotate | Download (6.76 KB)
1 |
/*
|
---|---|
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.sql._crypto"]){ |
9 |
dojo._hasResource["dojox.sql._crypto"]=true; |
10 |
dojo.provide("dojox.sql._crypto");
|
11 |
dojo.mixin(dojox.sql._crypto,{_POOL_SIZE:100,encrypt:function(_1,_2,_3){ |
12 |
this._initWorkerPool();
|
13 |
var _4={plaintext:_1,password:_2}; |
14 |
_4=dojo.toJson(_4); |
15 |
_4="encr:"+String(_4);
|
16 |
this._assignWork(_4,_3);
|
17 |
},decrypt:function(_5,_6,_7){ |
18 |
this._initWorkerPool();
|
19 |
var _8={ciphertext:_5,password:_6}; |
20 |
_8=dojo.toJson(_8); |
21 |
_8="decr:"+String(_8);
|
22 |
this._assignWork(_8,_7);
|
23 |
},_initWorkerPool:function(){ |
24 |
if(!this._manager){ |
25 |
try{
|
26 |
this._manager=google.gears.factory.create("beta.workerpool","1.0"); |
27 |
this._unemployed=[];
|
28 |
this._employed={};
|
29 |
this._handleMessage=[];
|
30 |
var _9=this; |
31 |
this._manager.onmessage=function(_a,_b){ |
32 |
var _c=_9._employed["_"+_b]; |
33 |
_9._employed["_"+_b]=undefined; |
34 |
_9._unemployed.push("_"+_b);
|
35 |
if(_9._handleMessage.length){
|
36 |
var _d=_9._handleMessage.shift();
|
37 |
_9._assignWork(_d.msg,_d.callback); |
38 |
} |
39 |
_c(_a); |
40 |
}; |
41 |
var _e="function _workerInit(){"+"gearsWorkerPool.onmessage = "+String(this._workerHandler)+";"+"}"; |
42 |
var _f=_e+" _workerInit();"; |
43 |
for(var i=0;i<this._POOL_SIZE;i++){ |
44 |
this._unemployed.push("_"+this._manager.createWorker(_f)); |
45 |
} |
46 |
} |
47 |
catch(exp){
|
48 |
throw exp.message||exp;
|
49 |
} |
50 |
} |
51 |
},_assignWork:function(msg,_10){ |
52 |
if(!this._handleMessage.length&&this._unemployed.length){ |
53 |
var _11=this._unemployed.shift().substring(1); |
54 |
this._employed["_"+_11]=_10; |
55 |
this._manager.sendMessage(msg,parseInt(_11,10)); |
56 |
}else{
|
57 |
this._handleMessage={msg:msg,callback:_10}; |
58 |
} |
59 |
},_workerHandler:function(msg,_12){ |
60 |
var _13=[99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22]; |
61 |
var _14=[[0,0,0,0],[1,0,0,0],[2,0,0,0],[4,0,0,0],[8,0,0,0],[16,0,0,0],[32,0,0,0],[64,0,0,0],[128,0,0,0],[27,0,0,0],[54,0,0,0]]; |
62 |
function _15(_16,w){ |
63 |
var Nb=4; |
64 |
var Nr=w.length/Nb-1; |
65 |
var _17=[[],[],[],[]];
|
66 |
for(var i=0;i<4*Nb;i++){ |
67 |
_17[i%4][Math.floor(i/4)]=_16[i]; |
68 |
} |
69 |
_17=_18(_17,w,0,Nb);
|
70 |
for(var _19=1;_19<Nr;_19++){ |
71 |
_17=_1a(_17,Nb); |
72 |
_17=_1b(_17,Nb); |
73 |
_17=_1c(_17,Nb); |
74 |
_17=_18(_17,w,_19,Nb); |
75 |
} |
76 |
_17=_1a(_17,Nb); |
77 |
_17=_1b(_17,Nb); |
78 |
_17=_18(_17,w,Nr,Nb); |
79 |
var _1d=new Array(4*Nb); |
80 |
for(var i=0;i<4*Nb;i++){ |
81 |
_1d[i]=_17[i%4][Math.floor(i/4)]; |
82 |
} |
83 |
return _1d;
|
84 |
}; |
85 |
function _1a(s,Nb){ |
86 |
for(var r=0;r<4;r++){ |
87 |
for(var c=0;c<Nb;c++){ |
88 |
s[r][c]=_13[s[r][c]]; |
89 |
} |
90 |
} |
91 |
return s;
|
92 |
}; |
93 |
function _1b(s,Nb){ |
94 |
var t=new Array(4); |
95 |
for(var r=1;r<4;r++){ |
96 |
for(var c=0;c<4;c++){ |
97 |
t[c]=s[r][(c+r)%Nb]; |
98 |
} |
99 |
for(var c=0;c<4;c++){ |
100 |
s[r][c]=t[c]; |
101 |
} |
102 |
} |
103 |
return s;
|
104 |
}; |
105 |
function _1c(s,Nb){ |
106 |
for(var c=0;c<4;c++){ |
107 |
var a=new Array(4); |
108 |
var b=new Array(4); |
109 |
for(var i=0;i<4;i++){ |
110 |
a[i]=s[i][c]; |
111 |
b[i]=s[i][c]&128?s[i][c]<<1^283:s[i][c]<<1; |
112 |
} |
113 |
s[0][c]=b[0]^a[1]^b[1]^a[2]^a[3]; |
114 |
s[1][c]=a[0]^b[1]^a[2]^b[2]^a[3]; |
115 |
s[2][c]=a[0]^a[1]^b[2]^a[3]^b[3]; |
116 |
s[3][c]=a[0]^b[0]^a[1]^a[2]^b[3]; |
117 |
} |
118 |
return s;
|
119 |
}; |
120 |
function _18(_1e,w,rnd,Nb){ |
121 |
for(var r=0;r<4;r++){ |
122 |
for(var c=0;c<Nb;c++){ |
123 |
_1e[r][c]^=w[rnd*4+c][r];
|
124 |
} |
125 |
} |
126 |
return _1e;
|
127 |
}; |
128 |
function _1f(key){ |
129 |
var Nb=4; |
130 |
var Nk=key.length/4; |
131 |
var Nr=Nk+6; |
132 |
var w=new Array(Nb*(Nr+1)); |
133 |
var _20=new Array(4); |
134 |
for(var i=0;i<Nk;i++){ |
135 |
var r=[key[4*i],key[4*i+1],key[4*i+2],key[4*i+3]]; |
136 |
w[i]=r; |
137 |
} |
138 |
for(var i=Nk;i<(Nb*(Nr+1));i++){ |
139 |
w[i]=new Array(4); |
140 |
for(var t=0;t<4;t++){ |
141 |
_20[t]=w[i-1][t];
|
142 |
} |
143 |
if(i%Nk==0){ |
144 |
_20=_21(_22(_20)); |
145 |
for(var t=0;t<4;t++){ |
146 |
_20[t]^=_14[i/Nk][t]; |
147 |
} |
148 |
}else{
|
149 |
if(Nk>6&&i%Nk==4){ |
150 |
_20=_21(_20); |
151 |
} |
152 |
} |
153 |
for(var t=0;t<4;t++){ |
154 |
w[i][t]=w[i-Nk][t]^_20[t]; |
155 |
} |
156 |
} |
157 |
return w;
|
158 |
}; |
159 |
function _21(w){ |
160 |
for(var i=0;i<4;i++){ |
161 |
w[i]=_13[w[i]]; |
162 |
} |
163 |
return w;
|
164 |
}; |
165 |
function _22(w){ |
166 |
w[4]=w[0]; |
167 |
for(var i=0;i<4;i++){ |
168 |
w[i]=w[i+1];
|
169 |
} |
170 |
return w;
|
171 |
}; |
172 |
function _23(_24,_25,_26){ |
173 |
if(!(_26==128||_26==192||_26==256)){ |
174 |
return ""; |
175 |
} |
176 |
var _27=_26/8; |
177 |
var _28=new Array(_27); |
178 |
for(var i=0;i<_27;i++){ |
179 |
_28[i]=_25.charCodeAt(i)&255;
|
180 |
} |
181 |
var key=_15(_28,_1f(_28));
|
182 |
key=key.concat(key.slice(0,_27-16)); |
183 |
var _29=16; |
184 |
var _2a=new Array(_29); |
185 |
var _2b=(new Date()).getTime(); |
186 |
for(var i=0;i<4;i++){ |
187 |
_2a[i]=(_2b>>>i*8)&255; |
188 |
} |
189 |
for(var i=0;i<4;i++){ |
190 |
_2a[i+4]=(_2b/4294967296>>>i*8)&255; |
191 |
} |
192 |
var _2c=_1f(key);
|
193 |
var _2d=Math.ceil(_24.length/_29);
|
194 |
var _2e=new Array(_2d); |
195 |
for(var b=0;b<_2d;b++){ |
196 |
for(var c=0;c<4;c++){ |
197 |
_2a[15-c]=(b>>>c*8)&255; |
198 |
} |
199 |
for(var c=0;c<4;c++){ |
200 |
_2a[15-c-4]=(b/4294967296>>>c*8); |
201 |
} |
202 |
var _2f=_15(_2a,_2c);
|
203 |
var _30=b<_2d-1?_29:(_24.length-1)%_29+1; |
204 |
var ct=""; |
205 |
for(var i=0;i<_30;i++){ |
206 |
var _31=_24.charCodeAt(b*_29+i);
|
207 |
var _32=_31^_2f[i];
|
208 |
ct+=String.fromCharCode(_32); |
209 |
} |
210 |
_2e[b]=_33(ct); |
211 |
} |
212 |
var _34=""; |
213 |
for(var i=0;i<8;i++){ |
214 |
_34+=String.fromCharCode(_2a[i]); |
215 |
} |
216 |
_34=_33(_34); |
217 |
return _34+"-"+_2e.join("-"); |
218 |
}; |
219 |
function _35(_36,_37,_38){ |
220 |
if(!(_38==128||_38==192||_38==256)){ |
221 |
return ""; |
222 |
} |
223 |
var _39=_38/8; |
224 |
var _3a=new Array(_39); |
225 |
for(var i=0;i<_39;i++){ |
226 |
_3a[i]=_37.charCodeAt(i)&255;
|
227 |
} |
228 |
var _3b=_1f(_3a);
|
229 |
var key=_15(_3a,_3b);
|
230 |
key=key.concat(key.slice(0,_39-16)); |
231 |
var _3c=_1f(key);
|
232 |
_36=_36.split("-");
|
233 |
var _3d=16; |
234 |
var _3e=new Array(_3d); |
235 |
var _3f=_40(_36[0]); |
236 |
for(var i=0;i<8;i++){ |
237 |
_3e[i]=_3f.charCodeAt(i); |
238 |
} |
239 |
var _41=new Array(_36.length-1); |
240 |
for(var b=1;b<_36.length;b++){ |
241 |
for(var c=0;c<4;c++){ |
242 |
_3e[15-c]=((b-1)>>>c*8)&255; |
243 |
} |
244 |
for(var c=0;c<4;c++){ |
245 |
_3e[15-c-4]=((b/4294967296-1)>>>c*8)&255; |
246 |
} |
247 |
var _42=_15(_3e,_3c);
|
248 |
_36[b]=_40(_36[b]); |
249 |
var pt=""; |
250 |
for(var i=0;i<_36[b].length;i++){ |
251 |
var _43=_36[b].charCodeAt(i);
|
252 |
var _44=_43^_42[i];
|
253 |
pt+=String.fromCharCode(_44); |
254 |
} |
255 |
_41[b-1]=pt;
|
256 |
} |
257 |
return _41.join(""); |
258 |
}; |
259 |
function _33(str){ |
260 |
return str.replace(/[\0\t\n\v\f\r\xa0!-]/g,function(c){ |
261 |
return "!"+c.charCodeAt(0)+"!"; |
262 |
}); |
263 |
}; |
264 |
function _40(str){ |
265 |
return str.replace(/!\d\d?\d?!/g,function(c){ |
266 |
return String.fromCharCode(c.slice(1,-1)); |
267 |
}); |
268 |
}; |
269 |
function _45(_46,_47){ |
270 |
return _23(_46,_47,256); |
271 |
}; |
272 |
function _48(_49,_4a){ |
273 |
return _35(_49,_4a,256); |
274 |
}; |
275 |
var cmd=msg.substr(0,4); |
276 |
var arg=msg.substr(5); |
277 |
if(cmd=="encr"){ |
278 |
arg=eval("("+arg+")"); |
279 |
var _4b=arg.plaintext;
|
280 |
var _4c=arg.password;
|
281 |
var _4d=_45(_4b,_4c);
|
282 |
gearsWorkerPool.sendMessage(String(_4d),_12); |
283 |
}else{
|
284 |
if(cmd=="decr"){ |
285 |
arg=eval("("+arg+")"); |
286 |
var _4e=arg.ciphertext;
|
287 |
var _4c=arg.password;
|
288 |
var _4d=_48(_4e,_4c);
|
289 |
gearsWorkerPool.sendMessage(String(_4d),_12); |
290 |
} |
291 |
} |
292 |
}}); |
293 |
} |