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