Subversion Repositories Sites.tela-botanica.org

Rev

Details | Last modification | View Log | RSS feed

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