4 * Javascript implementation of the RSA Data Security, Inc. MD5
5 * Message-Digest Algorithm.
7 * Copyright (c) 1996 Henri Torgemane. All Rights Reserved.
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.
14 * Of course, this soft is provided "as is" without express or implied
15 * warranty of any kind.
18 * Modified with german comments and some information about collisions.
19 * (Ralf Mieke, ralf@miekenet.de, http://mieke.home.pages.de)
20 * French translation: Serge François, serge@selfhtml.org, http://fr.selfhtml.org
26 for(i=0;i<n;i++) this[i]=0;
32 /* Quelques fonctions fondamentales doivent être transformées à cause
33 * d'erreurs Javascript.
34 * Essayez par exemple de calculer 0xffffffff >> 4 ...
35 * Les fonctions utilisées maintenant sont il est vrai plus lentes que les
36 * fonctions originales mais elles fonctionnent.
39 function integer(n) { return n%(0xffffffff+1); }
44 if (a-0x80000000>=0) {
55 if (a&0x40000000==0x40000000)
68 for (var i=0;i<b;i++) a=shl1(a);
75 var t1=(a-0x80000000);
76 var t2=(b-0x80000000);
79 return ((t1&t2)+0x80000000);
92 var t1=(a-0x80000000);
93 var t2=(b-0x80000000);
96 return ((t1|t2)+0x80000000);
98 return ((t1|b)+0x80000000);
101 return ((a|t2)+0x80000000);
109 var t1=(a-0x80000000);
110 var t2=(b-0x80000000);
115 return ((t1^b)+0x80000000);
118 return ((a^t2)+0x80000000);
125 return (0xffffffff-a);
128 /* Début de l'algorithme */
130 var state = new array(4);
131 var count = new array(2);
134 var buffer = new array(64);
135 var transformBuffer = new array(16);
136 var digestBits = new array(16);
156 return or(and(x,y),and(not(x),z));
160 return or(and(x,z),and(y,not(z)));
164 return xor(xor(x,y),z);
168 return xor(y ,or(x , not(z)));
171 function rotateLeft(a,n) {
172 return or(shl(a, n),(shr(a,(32 - n))));
175 function FF(a,b,c,d,x,s,ac) {
176 a = a+F(b, c, d) + x + ac;
177 a = rotateLeft(a, s);
182 function GG(a,b,c,d,x,s,ac) {
183 a = a+G(b, c, d) +x + ac;
184 a = rotateLeft(a, s);
189 function HH(a,b,c,d,x,s,ac) {
190 a = a+H(b, c, d) + x + ac;
191 a = rotateLeft(a, s);
196 function II(a,b,c,d,x,s,ac) {
197 a = a+I(b, c, d) + x + ac;
198 a = rotateLeft(a, s);
203 function transform(buf,offset) {
204 var a=0, b=0, c=0, d=0;
205 var x = transformBuffer;
212 for (i = 0; i < 16; i++) {
213 x[i] = and(buf[i*4+offset],0xff);
214 for (j = 1; j < 4; j++) {
215 x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
220 a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
221 d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
222 c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
223 b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
224 a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
225 d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
226 c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
227 b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
228 a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
229 d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
230 c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
231 b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
232 a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
233 d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
234 c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
235 b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
238 a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
239 d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
240 c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
241 b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
242 a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
243 d = GG ( d, a, b, c, x[10], S22, 0x2441453); /* 22 */
244 c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
245 b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
246 a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
247 d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
248 c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
249 b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
250 a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
251 d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
252 c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
253 b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
256 a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
257 d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
258 c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
259 b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
260 a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
261 d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
262 c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
263 b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
264 a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
265 d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
266 c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
267 b = HH ( b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
268 a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
269 d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
270 c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
271 b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
274 a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
275 d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
276 c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
277 b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
278 a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
279 d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
280 c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
281 b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
282 a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
283 d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
284 c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
285 b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
286 a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
287 d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
288 c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
289 b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
297 /* Avec l'initialisation de Dobbertin:
298 state[0] = 0x12ac2375;
299 state[1] = 0x3b341042;
300 state[2] = 0x5f62b97c;
301 state[3] = 0x4ba763ed;
302 s'il y a une collision:
305 M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
306 39GIK9>TF$W()/MEHR%C4:G1R:Q"=
311 M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
312 39GIK9>TF$W()/MEHREC4:G1R:Q"=
317 count[0]=count[1] = 0;
318 state[0] = 0x67452301;
319 state[1] = 0xefcdab89;
320 state[2] = 0x98badcfe;
321 state[3] = 0x10325476;
322 for (i = 0; i < digestBits.length; i++)
329 index = and(shr(count[0],3) , 0x3f);
330 if (count[0]<0xffffffff-7)
334 count[0]-=0xffffffff+1;
337 buffer[index] = and(b,0xff);
339 transform(buffer, 0);
344 var bits = new array(8);
346 var i=0, index=0, padLen=0;
348 for (i = 0; i < 4; i++) {
349 bits[i] = and(shr(count[0],(i * 8)), 0xff);
351 for (i = 0; i < 4; i++) {
352 bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
354 index = and(shr(count[0], 3) ,0x3f);
355 padLen = (index < 56) ? (56 - index) : (120 - index);
356 padding = new array(64);
358 for (i=0;i<padLen;i++)
363 for (i = 0; i < 4; i++) {
364 for (j = 0; j < 4; j++) {
365 digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
370 /* Fin de l'algorithme MD5 */
373 var hexa_h = "0123456789abcdef";
376 for (hexa_i=0;hexa_i<8;hexa_i++) {
377 hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
378 hexa_m=Math.floor(hexa_m/16);
384 var ascii="01234567890123456789012345678901" +
385 " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
386 "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
388 function MD5(message)
390 var l,s,k,ka,kb,kc,kd;
393 for (k=0;k<message.length;k++) {
395 update(ascii.lastIndexOf(l));
399 for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
400 for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
401 for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
402 for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
403 s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);