PageSourceSearch

https://www.mathnet.ru/js/md5.js

js mathnet.ru collected 2026-09-24 08:44:48 UTC 10,053 bytes, 382 lines download raw bytes

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

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.