PageSourceSearch

https://www.efmcshortcourses.org/md5.js?antdocvers=20031104105549

js efmcshortcourses.org collected 2026-09-26 16:51:09 UTC 9,611 bytes, 377 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.