cdf9e4ce84f7393d6275f1983cd85a86e82f84fc
[Packages/TYPO3.CMS.git] / typo3 / md5.js
1 /*
2 * md5.jvs 1.0b 27/06/96
3 *
4 * Javascript implementation of the RSA Data Security, Inc. MD5
5 * Message-Digest Algorithm.
6 *
7 * Copyright (c) 1996 Henri Torgemane. All Rights Reserved.
8 *
9 * Permission to use, copy, modify, and distribute this software
10 * and its documentation for any purposes and without
11 * fee is hereby granted provided that this copyright notice
12 * appears in all copies.
13 *
14 * Of course, this soft is provided "as is" without express or implied
15 * warranty of any kind.
16 */
17
18
19
20 function array(n) {
21 for(i=0;i<n;i++) this[i]=0;
22 this.length=n;
23 }
24
25 /* Some basic logical functions had to be rewritten because of a bug in
26 * Javascript.. Just try to compute 0xffffffff >> 4 with it..
27 * Of course, these functions are slower than the original would be, but
28 * at least, they work!
29 */
30
31 function integer(n) { return n%(0xffffffff+1); }
32
33 function shr(a,b) {
34 a=integer(a);
35 b=integer(b);
36 if (a-0x80000000>=0) {
37 a=a%0x80000000;
38 a>>=b;
39 a+=0x40000000>>(b-1);
40 } else
41 a>>=b;
42 return a;
43 }
44
45 function shl1(a) {
46 a=a%0x80000000;
47 if (a&0x40000000==0x40000000)
48 {
49 a-=0x40000000;
50 a*=2;
51 a+=0x80000000;
52 } else
53 a*=2;
54 return a;
55 }
56
57 function shl(a,b) {
58 a=integer(a);
59 b=integer(b);
60 for (var i=0;i<b;i++) a=shl1(a);
61 return a;
62 }
63
64 function and(a,b) {
65 a=integer(a);
66 b=integer(b);
67 var t1=(a-0x80000000);
68 var t2=(b-0x80000000);
69 if (t1>=0)
70 if (t2>=0)
71 return ((t1&t2)+0x80000000);
72 else
73 return (t1&b);
74 else
75 if (t2>=0)
76 return (a&t2);
77 else
78 return (a&b);
79 }
80
81 function or(a,b) {
82 a=integer(a);
83 b=integer(b);
84 var t1=(a-0x80000000);
85 var t2=(b-0x80000000);
86 if (t1>=0)
87 if (t2>=0)
88 return ((t1|t2)+0x80000000);
89 else
90 return ((t1|b)+0x80000000);
91 else
92 if (t2>=0)
93 return ((a|t2)+0x80000000);
94 else
95 return (a|b);
96 }
97
98 function xor(a,b) {
99 a=integer(a);
100 b=integer(b);
101 var t1=(a-0x80000000);
102 var t2=(b-0x80000000);
103 if (t1>=0)
104 if (t2>=0)
105 return (t1^t2);
106 else
107 return ((t1^b)+0x80000000);
108 else
109 if (t2>=0)
110 return ((a^t2)+0x80000000);
111 else
112 return (a^b);
113 }
114
115 function not(a) {
116 a=integer(a);
117 return (0xffffffff-a);
118 }
119
120 /* Here begin the real algorithm */
121
122 var state = new array(4);
123 var count = new array(2);
124 count[0] = 0;
125 count[1] = 0;
126 var buffer = new array(64);
127 var transformBuffer = new array(16);
128 var digestBits = new array(16);
129
130 var S11 = 7;
131 var S12 = 12;
132 var S13 = 17;
133 var S14 = 22;
134 var S21 = 5;
135 var S22 = 9;
136 var S23 = 14;
137 var S24 = 20;
138 var S31 = 4;
139 var S32 = 11;
140 var S33 = 16;
141 var S34 = 23;
142 var S41 = 6;
143 var S42 = 10;
144 var S43 = 15;
145 var S44 = 21;
146
147 function F(x,y,z) {
148 return or(and(x,y),and(not(x),z));
149 }
150
151 function G(x,y,z) {
152 return or(and(x,z),and(y,not(z)));
153 }
154
155 function H(x,y,z) {
156 return xor(xor(x,y),z);
157 }
158
159 function I(x,y,z) {
160 return xor(y ,or(x , not(z)));
161 }
162
163 function rotateLeft(a,n) {
164 return or(shl(a, n),(shr(a,(32 - n))));
165 }
166
167 function FF(a,b,c,d,x,s,ac) {
168 a = a+F(b, c, d) + x + ac;
169 a = rotateLeft(a, s);
170 a = a+b;
171 return a;
172 }
173
174 function GG(a,b,c,d,x,s,ac) {
175 a = a+G(b, c, d) +x + ac;
176 a = rotateLeft(a, s);
177 a = a+b;
178 return a;
179 }
180
181 function HH(a,b,c,d,x,s,ac) {
182 a = a+H(b, c, d) + x + ac;
183 a = rotateLeft(a, s);
184 a = a+b;
185 return a;
186 }
187
188 function II(a,b,c,d,x,s,ac) {
189 a = a+I(b, c, d) + x + ac;
190 a = rotateLeft(a, s);
191 a = a+b;
192 return a;
193 }
194
195 function transform(buf,offset) {
196 var a=0, b=0, c=0, d=0;
197 var x = transformBuffer;
198
199 a = state[0];
200 b = state[1];
201 c = state[2];
202 d = state[3];
203
204 for (i = 0; i < 16; i++) {
205 x[i] = and(buf[i*4+offset],0xff);
206 for (j = 1; j < 4; j++) {
207 x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
208 }
209 }
210
211 /* Round 1 */
212 a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
213 d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
214 c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
215 b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
216 a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
217 d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
218 c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
219 b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
220 a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
221 d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
222 c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
223 b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
224 a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
225 d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
226 c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
227 b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
228
229 /* Round 2 */
230 a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
231 d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
232 c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
233 b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
234 a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
235 d = GG ( d, a, b, c, x[10], S22, 0x2441453); /* 22 */
236 c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
237 b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
238 a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
239 d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
240 c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
241 b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
242 a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
243 d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
244 c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
245 b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
246
247 /* Round 3 */
248 a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
249 d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
250 c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
251 b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
252 a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
253 d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
254 c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
255 b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
256 a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
257 d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
258 c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
259 b = HH ( b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
260 a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
261 d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
262 c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
263 b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
264
265 /* Round 4 */
266 a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
267 d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
268 c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
269 b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
270 a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
271 d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
272 c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
273 b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
274 a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
275 d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
276 c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
277 b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
278 a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
279 d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
280 c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
281 b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
282
283 state[0] +=a;
284 state[1] +=b;
285 state[2] +=c;
286 state[3] +=d;
287
288 }
289
290 function init() {
291 count[0]=count[1] = 0;
292 state[0] = 0x67452301;
293 state[1] = 0xefcdab89;
294 state[2] = 0x98badcfe;
295 state[3] = 0x10325476;
296 for (i = 0; i < digestBits.length; i++)
297 digestBits[i] = 0;
298 }
299
300 function update(b) {
301 var index,i;
302
303 index = and(shr(count[0],3) , 0x3f);
304 if (count[0]<0xffffffff-7)
305 count[0] += 8;
306 else {
307 count[1]++;
308 count[0]-=0xffffffff+1;
309 count[0]+=8;
310 }
311 buffer[index] = and(b,0xff);
312 if (index >= 63) {
313 transform(buffer, 0);
314 }
315 }
316
317 function finish() {
318 var bits = new array(8);
319 var padding;
320 var i=0, index=0, padLen=0;
321
322 for (i = 0; i < 4; i++) {
323 bits[i] = and(shr(count[0],(i * 8)), 0xff);
324 }
325 for (i = 0; i < 4; i++) {
326 bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
327 }
328 index = and(shr(count[0], 3) ,0x3f);
329 padLen = (index < 56) ? (56 - index) : (120 - index);
330 padding = new array(64);
331 padding[0] = 0x80;
332 for (i=0;i<padLen;i++)
333 update(padding[i]);
334 for (i=0;i<8;i++)
335 update(bits[i]);
336
337 for (i = 0; i < 4; i++) {
338 for (j = 0; j < 4; j++) {
339 digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
340 }
341 }
342 }
343
344 /* End of the MD5 algorithm */
345
346 function hexa(n) {
347 var hexa_h = "0123456789abcdef";
348 var hexa_c="";
349 var hexa_m=n;
350 for (hexa_i=0;hexa_i<8;hexa_i++) {
351 hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
352 hexa_m=Math.floor(hexa_m/16);
353 }
354 return hexa_c;
355 }
356
357
358 var ascii="01234567890123456789012345678901" +
359 " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
360 "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
361
362 function MD5(entree)
363 {
364 var l,s,k,ka,kb,kc,kd;
365
366 init();
367 for (k=0;k<entree.length;k++) {
368 l=entree.charAt(k);
369 update(ascii.lastIndexOf(l));
370 }
371 finish();
372 ka=kb=kc=kd=0;
373 for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
374 for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
375 for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
376 for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
377 s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
378 return s;
379 }