File indexing completed on 2024-05-05 06:02:11

0001 /*
0002  *  md5.js 1.0b 27/06/96
0003  *
0004  * Javascript implementation of the RSA Data Security, Inc. MD5
0005  * Message-Digest Algorithm.
0006  *
0007  * Copyright (c) 1996 Henri Torgemane. All Rights Reserved.
0008  *
0009  * Permission to use, copy, modify, and distribute this software
0010  * and its documentation for any purposes and without
0011  * fee is hereby granted provided that this copyright notice
0012  * appears in all copies.
0013  *
0014  * Of course, this soft is provided "as is" without express or implied
0015  * warranty of any kind.
0016  *
0017  *
0018  * Modified with german comments and some information about collisions.
0019  * (Ralf Mieke, ralf@miekenet.de, http://mieke.home.pages.de)
0020  */
0021 
0022 
0023 
0024 function array(n) {
0025   for(i=0;i<n;i++) this[i]=0;
0026   this.length=n;
0027 }
0028 
0029 
0030 
0031 /* Einige grundlegenden Funktionen müssen wegen
0032  * Javascript Fehlern umgeschrieben werden.
0033  * Man versuche z.B. 0xffffffff >> 4 zu berechnen..
0034  * Die nun verwendeten Funktionen sind zwar langsamer als die Originale,
0035  * aber sie funktionieren.
0036  */
0037 
0038 function integer(n) { return n%(0xffffffff+1); }
0039 
0040 function shr(a,b) {
0041   a=integer(a);
0042   b=integer(b);
0043   if (a-0x80000000>=0) {
0044     a=a%0x80000000;
0045     a>>=b;
0046     a+=0x40000000>>(b-1);
0047   } else
0048     a>>=b;
0049   return a;
0050 }
0051 
0052 function shl1(a) {
0053   a=a%0x80000000;
0054   if (a&0x40000000==0x40000000)
0055   {
0056     a-=0x40000000;
0057     a*=2;
0058     a+=0x80000000;
0059   } else
0060     a*=2;
0061   return a;
0062 }
0063 
0064 function shl(a,b) {
0065   a=integer(a);
0066   b=integer(b);
0067   for (var i=0;i<b;i++) a=shl1(a);
0068   return a;
0069 }
0070 
0071 function and(a,b) {
0072   a=integer(a);
0073   b=integer(b);
0074   var t1=(a-0x80000000);
0075   var t2=(b-0x80000000);
0076   if (t1>=0)
0077     if (t2>=0)
0078       return ((t1&t2)+0x80000000);
0079     else
0080       return (t1&b);
0081   else
0082     if (t2>=0)
0083       return (a&t2);
0084     else
0085       return (a&b);
0086 }
0087 
0088 function or(a,b) {
0089   a=integer(a);
0090   b=integer(b);
0091   var t1=(a-0x80000000);
0092   var t2=(b-0x80000000);
0093   if (t1>=0)
0094     if (t2>=0)
0095       return ((t1|t2)+0x80000000);
0096     else
0097       return ((t1|b)+0x80000000);
0098   else
0099     if (t2>=0)
0100       return ((a|t2)+0x80000000);
0101     else
0102       return (a|b);
0103 }
0104 
0105 function xor(a,b) {
0106   a=integer(a);
0107   b=integer(b);
0108   var t1=(a-0x80000000);
0109   var t2=(b-0x80000000);
0110   if (t1>=0)
0111     if (t2>=0)
0112       return (t1^t2);
0113     else
0114       return ((t1^b)+0x80000000);
0115   else
0116     if (t2>=0)
0117       return ((a^t2)+0x80000000);
0118     else
0119       return (a^b);
0120 }
0121 
0122 function not(a) {
0123   a=integer(a);
0124   return (0xffffffff-a);
0125 }
0126 
0127 /* Beginn des Algorithmus */
0128 
0129     var state = new array(4);
0130     var count = new array(2);
0131         count[0] = 0;
0132         count[1] = 0;
0133     var buffer = new array(64);
0134     var transformBuffer = new array(16);
0135     var digestBits = new array(16);
0136 
0137     var S11 = 7;
0138     var S12 = 12;
0139     var S13 = 17;
0140     var S14 = 22;
0141     var S21 = 5;
0142     var S22 = 9;
0143     var S23 = 14;
0144     var S24 = 20;
0145     var S31 = 4;
0146     var S32 = 11;
0147     var S33 = 16;
0148     var S34 = 23;
0149     var S41 = 6;
0150     var S42 = 10;
0151     var S43 = 15;
0152     var S44 = 21;
0153 
0154     function F(x,y,z) {
0155         return or(and(x,y),and(not(x),z));
0156     }
0157 
0158     function G(x,y,z) {
0159         return or(and(x,z),and(y,not(z)));
0160     }
0161 
0162     function H(x,y,z) {
0163         return xor(xor(x,y),z);
0164     }
0165 
0166     function I(x,y,z) {
0167         return xor(y ,or(x , not(z)));
0168     }
0169 
0170     function rotateLeft(a,n) {
0171         return or(shl(a, n),(shr(a,(32 - n))));
0172     }
0173 
0174     function FF(a,b,c,d,x,s,ac) {
0175         a = a+F(b, c, d) + x + ac;
0176         a = rotateLeft(a, s);
0177         a = a+b;
0178         return a;
0179     }
0180 
0181     function GG(a,b,c,d,x,s,ac) {
0182         a = a+G(b, c, d) +x + ac;
0183         a = rotateLeft(a, s);
0184         a = a+b;
0185         return a;
0186     }
0187 
0188     function HH(a,b,c,d,x,s,ac) {
0189         a = a+H(b, c, d) + x + ac;
0190         a = rotateLeft(a, s);
0191         a = a+b;
0192         return a;
0193     }
0194 
0195     function II(a,b,c,d,x,s,ac) {
0196         a = a+I(b, c, d) + x + ac;
0197         a = rotateLeft(a, s);
0198         a = a+b;
0199         return a;
0200     }
0201 
0202     function transform(buf,offset) {
0203         var a=0, b=0, c=0, d=0;
0204         var x = transformBuffer;
0205 
0206         a = state[0];
0207         b = state[1];
0208         c = state[2];
0209         d = state[3];
0210 
0211         for (i = 0; i < 16; i++) {
0212             x[i] = and(buf[i*4+offset],0xff);
0213             for (j = 1; j < 4; j++) {
0214                 x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
0215             }
0216         }
0217 
0218         /* Runde 1 */
0219         a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
0220         d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
0221         c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
0222         b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
0223         a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
0224         d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
0225         c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
0226         b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
0227         a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
0228         d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
0229         c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
0230         b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
0231         a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
0232         d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
0233         c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
0234         b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
0235 
0236         /* Runde 2 */
0237         a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
0238         d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
0239         c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
0240         b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
0241         a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
0242         d = GG ( d, a, b, c, x[10], S22,  0x2441453); /* 22 */
0243         c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
0244         b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
0245         a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
0246         d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
0247         c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
0248         b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
0249         a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
0250         d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
0251         c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
0252         b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
0253 
0254         /* Runde 3 */
0255         a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
0256         d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
0257         c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
0258         b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
0259         a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
0260         d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
0261         c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
0262         b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
0263         a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
0264         d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
0265         c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
0266         b = HH ( b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
0267         a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
0268         d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
0269         c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
0270         b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
0271 
0272         /* Runde 4 */
0273         a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
0274         d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
0275         c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
0276         b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
0277         a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
0278         d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
0279         c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
0280         b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
0281         a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
0282         d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
0283         c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
0284         b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
0285         a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
0286         d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
0287         c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
0288         b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
0289 
0290         state[0] +=a;
0291         state[1] +=b;
0292         state[2] +=c;
0293         state[3] +=d;
0294 
0295     }
0296     /* Mit der Initialisierung von Dobbertin:
0297        state[0] = 0x12ac2375;
0298        state[1] = 0x3b341042;
0299        state[2] = 0x5f62b97c;
0300        state[3] = 0x4ba763ed;
0301        gibt es eine Kollision:
0302 
0303        begin 644 Message1
0304        M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
0305        39GIK9>TF$W()/MEHR%C4:G1R:Q"=
0306        `
0307        end
0308 
0309        begin 644 Message2
0310        M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
0311        39GIK9>TF$W()/MEHREC4:G1R:Q"=
0312        `
0313        end
0314     */
0315     function init() {
0316         count[0]=count[1] = 0;
0317         state[0] = 0x67452301;
0318         state[1] = 0xefcdab89;
0319         state[2] = 0x98badcfe;
0320         state[3] = 0x10325476;
0321         for (i = 0; i < digestBits.length; i++)
0322             digestBits[i] = 0;
0323     }
0324 
0325     function update(b) {
0326         var index,i;
0327 
0328         index = and(shr(count[0],3) , 0x3f);
0329         if (count[0]<0xffffffff-7)
0330           count[0] += 8;
0331         else {
0332           count[1]++;
0333           count[0]-=0xffffffff+1;
0334           count[0]+=8;
0335         }
0336         buffer[index] = and(b,0xff);
0337         if (index  >= 63) {
0338             transform(buffer, 0);
0339         }
0340     }
0341 
0342     function finish() {
0343         var bits = new array(8);
0344         var        padding;
0345         var        i=0, index=0, padLen=0;
0346 
0347         for (i = 0; i < 4; i++) {
0348             bits[i] = and(shr(count[0],(i * 8)), 0xff);
0349         }
0350         for (i = 0; i < 4; i++) {
0351             bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
0352         }
0353         index = and(shr(count[0], 3) ,0x3f);
0354         padLen = (index < 56) ? (56 - index) : (120 - index);
0355         padding = new array(64);
0356         padding[0] = 0x80;
0357         for (i=0;i<padLen;i++)
0358           update(padding[i]);
0359         for (i=0;i<8;i++)
0360           update(bits[i]);
0361 
0362         for (i = 0; i < 4; i++) {
0363             for (j = 0; j < 4; j++) {
0364                 digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
0365             }
0366         }
0367     }
0368 
0369 /* Ende des MD5 Algorithmus */
0370 
0371 function hexa(n) {
0372  var hexa_h = "0123456789abcdef";
0373  var hexa_c="";
0374  var hexa_m=n;
0375  for (hexa_i=0;hexa_i<8;hexa_i++) {
0376    hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
0377    hexa_m=Math.floor(hexa_m/16);
0378  }
0379  return hexa_c;
0380 }
0381 
0382 
0383 var ascii="01234567890123456789012345678901" +
0384           " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
0385           "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
0386 
0387 function MD5(nachricht)
0388 {
0389  var l,s,k,ka,kb,kc,kd;
0390 
0391  init();
0392  for (k=0;k<nachricht.length;k++) {
0393    l=nachricht.charAt(k);
0394    update(ascii.lastIndexOf(l));
0395  }
0396  finish();
0397  ka=kb=kc=kd=0;
0398  for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
0399  for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
0400  for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
0401  for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
0402  s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
0403  return s;
0404 }