a
[learning-git.git] / distajax / example.py
blob5041ced85a6c9b2625cc40b8fc27cccd049a35ae
1 # coding:utf-8
2 import server
3 import thread
5 library = '''
6 <!--
7 /*
8 * md5.jvs 1.0b 27/06/96
10 * Javascript implementation of the RSA Data Security, Inc. MD5
11 * Message-Digest Algorithm.
13 * Copyright (c) 1996 Henri Torgemane. All Rights Reserved.
15 * Permission to use, copy, modify, and distribute this software
16 * and its documentation for any purposes and without
17 * fee is hereby granted provided that this copyright notice
18 * appears in all copies.
20 * Of course, this soft is provided "as is" without express or implied
21 * warranty of any kind.
24 * Modified with german comments and some information about collisions.
25 * (Ralf Mieke, ralf@miekenet.de, http://mieke.home.pages.de)
28 function array(n) {
29 for(i=0;i<n;i++) this[i]=0;
30 this.length=n;
33 /* Einige grundlegenden Funktionen müssen wegen
34 * Javascript Fehlern umgeschrieben werden.
35 * Man versuche z.B. 0xffffffff >> 4 zu berechnen..
36 * Die nun verwendeten Funktionen sind zwar langsamer als die Originale,
37 * aber sie funktionieren.
40 function integer(n) { return n%(0xffffffff+1); }
42 function shr(a,b) {
43 a=integer(a);
44 b=integer(b);
45 if (a-0x80000000>=0) {
46 a=a%0x80000000;
47 a>>=b;
48 a+=0x40000000>>(b-1);
49 } else
50 a>>=b;
51 return a;
54 function shl1(a) {
55 a=a%0x80000000;
56 if (a&0x40000000==0x40000000)
58 a-=0x40000000;
59 a*=2;
60 a+=0x80000000;
61 } else
62 a*=2;
63 return a;
66 function shl(a,b) {
67 a=integer(a);
68 b=integer(b);
69 for (var i=0;i<b;i++) a=shl1(a);
70 return a;
73 function and(a,b) {
74 a=integer(a);
75 b=integer(b);
76 var t1=(a-0x80000000);
77 var t2=(b-0x80000000);
78 if (t1>=0)
79 if (t2>=0)
80 return ((t1&t2)+0x80000000);
81 else
82 return (t1&b);
83 else
84 if (t2>=0)
85 return (a&t2);
86 else
87 return (a&b);
90 function or(a,b) {
91 a=integer(a);
92 b=integer(b);
93 var t1=(a-0x80000000);
94 var t2=(b-0x80000000);
95 if (t1>=0)
96 if (t2>=0)
97 return ((t1|t2)+0x80000000);
98 else
99 return ((t1|b)+0x80000000);
100 else
101 if (t2>=0)
102 return ((a|t2)+0x80000000);
103 else
104 return (a|b);
107 function xor(a,b) {
108 a=integer(a);
109 b=integer(b);
110 var t1=(a-0x80000000);
111 var t2=(b-0x80000000);
112 if (t1>=0)
113 if (t2>=0)
114 return (t1^t2);
115 else
116 return ((t1^b)+0x80000000);
117 else
118 if (t2>=0)
119 return ((a^t2)+0x80000000);
120 else
121 return (a^b);
124 function not(a) {
125 a=integer(a);
126 return (0xffffffff-a);
129 /* Beginn des Algorithmus */
131 var state = new array(4);
132 var count = new array(2);
133 count[0] = 0;
134 count[1] = 0;
135 var buffer = new array(64);
136 var transformBuffer = new array(16);
137 var digestBits = new array(16);
139 var S11 = 7;
140 var S12 = 12;
141 var S13 = 17;
142 var S14 = 22;
143 var S21 = 5;
144 var S22 = 9;
145 var S23 = 14;
146 var S24 = 20;
147 var S31 = 4;
148 var S32 = 11;
149 var S33 = 16;
150 var S34 = 23;
151 var S41 = 6;
152 var S42 = 10;
153 var S43 = 15;
154 var S44 = 21;
156 function F(x,y,z) {
157 return or(and(x,y),and(not(x),z));
160 function G(x,y,z) {
161 return or(and(x,z),and(y,not(z)));
164 function H(x,y,z) {
165 return xor(xor(x,y),z);
168 function I(x,y,z) {
169 return xor(y ,or(x , not(z)));
172 function rotateLeft(a,n) {
173 return or(shl(a, n),(shr(a,(32 - n))));
176 function FF(a,b,c,d,x,s,ac) {
177 a = a+F(b, c, d) + x + ac;
178 a = rotateLeft(a, s);
179 a = a+b;
180 return a;
183 function GG(a,b,c,d,x,s,ac) {
184 a = a+G(b, c, d) +x + ac;
185 a = rotateLeft(a, s);
186 a = a+b;
187 return a;
190 function HH(a,b,c,d,x,s,ac) {
191 a = a+H(b, c, d) + x + ac;
192 a = rotateLeft(a, s);
193 a = a+b;
194 return a;
197 function II(a,b,c,d,x,s,ac) {
198 a = a+I(b, c, d) + x + ac;
199 a = rotateLeft(a, s);
200 a = a+b;
201 return a;
204 function transform(buf,offset) {
205 var a=0, b=0, c=0, d=0;
206 var x = transformBuffer;
208 a = state[0];
209 b = state[1];
210 c = state[2];
211 d = state[3];
213 for (i = 0; i < 16; i++) {
214 x[i] = and(buf[i*4+offset],0xff);
215 for (j = 1; j < 4; j++) {
216 x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
220 /* Runde 1 */
221 a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
222 d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
223 c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
224 b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
225 a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
226 d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
227 c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
228 b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
229 a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
230 d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
231 c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
232 b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
233 a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
234 d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
235 c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
236 b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
238 /* Runde 2 */
239 a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
240 d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
241 c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
242 b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
243 a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
244 d = GG ( d, a, b, c, x[10], S22, 0x2441453); /* 22 */
245 c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
246 b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
247 a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
248 d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
249 c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
250 b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
251 a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
252 d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
253 c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
254 b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
256 /* Runde 3 */
257 a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
258 d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
259 c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
260 b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
261 a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
262 d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
263 c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
264 b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
265 a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
266 d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
267 c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
268 b = HH ( b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
269 a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
270 d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
271 c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
272 b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
274 /* Runde 4 */
275 a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
276 d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
277 c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
278 b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
279 a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
280 d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
281 c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
282 b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
283 a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
284 d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
285 c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
286 b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
287 a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
288 d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
289 c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
290 b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
292 state[0] +=a;
293 state[1] +=b;
294 state[2] +=c;
295 state[3] +=d;
298 /* Mit der Initialisierung von Dobbertin:
299 state[0] = 0x12ac2375;
300 state[1] = 0x3b341042;
301 state[2] = 0x5f62b97c;
302 state[3] = 0x4ba763ed;
303 gibt es eine Kollision:
305 begin 644 Message1
306 M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
307 39GIK9>TF$W()/MEHR%C4:G1R:Q"=
310 ^ man achte auf den Unterschied
311 begin 644 Message2
312 M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
313 39GIK9>TF$W()/MEHREC4:G1R:Q"=
316 ^ das Zeichen E und kein Prozent Zeichen
318 function init() {
319 count[0]=count[1] = 0;
320 state[0] = 0x67452301;
321 state[1] = 0xefcdab89;
322 state[2] = 0x98badcfe;
323 state[3] = 0x10325476;
324 for (i = 0; i < digestBits.length; i++)
325 digestBits[i] = 0;
328 function update(b) {
329 var index,i;
331 index = and(shr(count[0],3) , 0x3f);
332 if (count[0]<0xffffffff-7)
333 count[0] += 8;
334 else {
335 count[1]++;
336 count[0]-=0xffffffff+1;
337 count[0]+=8;
339 buffer[index] = and(b,0xff);
340 if (index >= 63) {
341 transform(buffer, 0);
345 function finish() {
346 var bits = new array(8);
347 var padding;
348 var i=0, index=0, padLen=0;
350 for (i = 0; i < 4; i++) {
351 bits[i] = and(shr(count[0],(i * 8)), 0xff);
353 for (i = 0; i < 4; i++) {
354 bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
356 index = and(shr(count[0], 3) ,0x3f);
357 padLen = (index < 56) ? (56 - index) : (120 - index);
358 padding = new array(64);
359 padding[0] = 0x80;
360 for (i=0;i<padLen;i++)
361 update(padding[i]);
362 for (i=0;i<8;i++)
363 update(bits[i]);
365 for (i = 0; i < 4; i++) {
366 for (j = 0; j < 4; j++) {
367 digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
372 /* Ende des MD5 Algorithmus */
374 function hexa(n) {
375 var hexa_h = "0123456789abcdef";
376 var hexa_c="";
377 var hexa_m=n;
378 for (hexa_i=0;hexa_i<8;hexa_i++) {
379 hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
380 hexa_m=Math.floor(hexa_m/16);
382 return hexa_c;
386 var ascii="01234567890123456789012345678901" +
387 " !\\"#$%&'()*+,-./" +
388 "0123456789" +
389 ":;<=>?@" +
390 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
391 "[\\\\]^_`" +
392 "abcdefghijklmnopqrstuvwxyz" +
393 "{|}~";
395 function MD5(nachricht)
397 var l,s,k,ka,kb,kc,kd;
399 init();
400 for (k=0;k<nachricht.length;k++) {
401 l=nachricht.charAt(k);
402 update(ascii.lastIndexOf(l));
404 finish();
405 ka=kb=kc=kd=0;
406 for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
407 for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
408 for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
409 for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
410 s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
411 return s;
414 //-->
417 server.library.add(library)
419 class BBPTask(server.Task):
420 def __init__(self,args):
421 server.Task.__init__(self, "MD5(%s)" % (",".join(args)))
422 self.args = args
423 def __repr__(self):
424 if len(self.solutions) > 0:
425 for solution in self.solutions:
426 return "MD5(%s) = %s" % (",".join(self.args), solution)
427 else:
428 return "MD5(%s) = ?" % (",".join(self.args))
430 def add_result(self,result):
431 server.Task.add_result(self,result)
432 print >> sys.stderr, "%s=%s" % (self.args[0], result)
435 for i in range(0,500):
436 server.jobs.storage.unsolved.append(BBPTask(["\"%s\""%i]))
438 #for word in open("/usr/share/dict/ngerman").read().split("\n"):
439 # server.jobs.storage.unsolved.append(BBPTask(["\"%s\""%word]))
443 server.run(threaded=True)
444 import sys
445 while True && __name__ == "__main__":
446 line = sys.stdin.readline().strip()
447 if line == "exit":
448 sys.exit(0)
449 elif line in ["help", "?"]:
450 print "save=filename\nhelp\nexit\n"
451 elif line.split("=")[0] == "save":
452 args = line.split("=")
453 if len(args) != 2:
454 print "bad argument, use save=filename"
455 else:
456 server.jobs.storage.save(args[1])
457 else:
458 print "solved=%s/waiting=%s/unsolved=%s" % (
459 len(server.jobs.storage.solved), len(server.jobs.storage.waiting), len(server.jobs.storage.unsolved))