1// Copyright 2013 the V8 project authors. All rights reserved. 2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions 6// are met: 7// 1. Redistributions of source code must retain the above copyright 8// notice, this list of conditions and the following disclaimer. 9// 2. Redistributions in binary form must reproduce the above copyright 10// notice, this list of conditions and the following disclaimer in the 11// documentation and/or other materials provided with the distribution. 12// 13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24description("KDE JS Test"); 25// shouldBe() (base.js) test at very end of this file (Harri) 26 27/* 28* md5.jvs 1.0b 27/06/96 29* 30* Javascript implementation of the RSA Data Security, Inc. MD5 31* Message-Digest Algorithm. 32* 33* Copyright (c) 1996 Henri Torgemane. All Rights Reserved. 34* 35* Permission to use, copy, modify, and distribute this software 36* and its documentation for any purposes and without 37* fee is hereby granted provided that this copyright notice 38* appears in all copies. 39* 40* Of course, this soft is provided "as is" without express or implied 41* warranty of any kind. 42 43*/ 44 45// $Id: md5-1.js 11771 2005-12-26 23:07:31Z mjs $ 46 47function array(n) { 48 for(i=0;i<n;i++) this[i]=0; 49 this.length=n; 50} 51 52/* Some basic logical functions had to be rewritten because of a bug in 53* Javascript.. Just try to compute 0xffffffff >> 4 with it.. 54* Of course, these functions are slower than the original would be, but 55* at least, they work! 56*/ 57 58function integer(n) { return n%(0xffffffff+1); } 59 60function shr(a,b) { 61 a=integer(a); 62 b=integer(b); 63 if (a-0x80000000>=0) { 64 a=a%0x80000000; 65 a>>=b; 66 a+=0x40000000>>(b-1); 67 } else 68 a>>=b; 69 return a; 70} 71 72function shl1(a) { 73 a=a%0x80000000; 74 if (a&0x40000000==0x40000000) 75 { 76 a-=0x40000000; 77 a*=2; 78 a+=0x80000000; 79 } else 80 a*=2; 81 return a; 82} 83 84function shl(a,b) { 85 a=integer(a); 86 b=integer(b); 87 for (var i=0;i<b;i++) a=shl1(a); 88 return a; 89} 90 91function and(a,b) { 92 a=integer(a); 93 b=integer(b); 94 var t1=(a-0x80000000); 95 var t2=(b-0x80000000); 96 if (t1>=0) 97 if (t2>=0) 98 return ((t1&t2)+0x80000000); 99 else 100 return (t1&b); 101 else 102 if (t2>=0) 103 return (a&t2); 104 else 105 return (a&b); 106} 107 108function or(a,b) { 109 a=integer(a); 110 b=integer(b); 111 var t1=(a-0x80000000); 112 var t2=(b-0x80000000); 113 if (t1>=0) 114 if (t2>=0) 115 return ((t1|t2)+0x80000000); 116 else 117 return ((t1|b)+0x80000000); 118 else 119 if (t2>=0) 120 return ((a|t2)+0x80000000); 121 else 122 return (a|b); 123} 124 125function xor(a,b) { 126 a=integer(a); 127 b=integer(b); 128 var t1=(a-0x80000000); 129 var t2=(b-0x80000000); 130 if (t1>=0) 131 if (t2>=0) 132 return (t1^t2); 133 else 134 return ((t1^b)+0x80000000); 135 else 136 if (t2>=0) 137 return ((a^t2)+0x80000000); 138 else 139 return (a^b); 140} 141 142 143function not(a) { 144 a=integer(a); 145 return (0xffffffff-a); 146} 147 148/* Here begin the real algorithm */ 149 150var state = new array(4); 151var count = new array(2); 152count[0] = 0; 153count[1] = 0; 154 155var buffer = new array(64); 156var transformBuffer = new array(16); 157var digestBits = new array(16); 158 159var S11 = 7; 160var S12 = 12; 161var S13 = 17; 162var S14 = 22; 163var S21 = 5; 164var S22 = 9; 165var S23 = 14; 166var S24 = 20; 167var S31 = 4; 168var S32 = 11; 169var S33 = 16; 170var S34 = 23; 171var S41 = 6; 172var S42 = 10; 173var S43 = 15; 174var S44 = 21; 175 176 177function F(x,y,z) { 178 return or(and(x,y),and(not(x),z)); 179} 180 181function G(x,y,z) { 182 return or(and(x,z),and(y,not(z))); 183} 184 185function H(x,y,z) { 186 return xor(xor(x,y),z); 187} 188 189function I(x,y,z) { 190 return xor(y ,or(x , not(z))); 191} 192 193function rotateLeft(a,n) { 194 return or(shl(a, n),(shr(a,(32 - n)))); 195} 196 197function FF(a,b,c,d,x,s,ac) { 198 a = a+F(b, c, d) + x + ac; 199 a = rotateLeft(a, s); 200 a = a+b; 201 return a; 202} 203 204function GG(a,b,c,d,x,s,ac) { 205 a = a+G(b, c, d) +x + ac; 206 a = rotateLeft(a, s); 207 a = a+b; 208 return a; 209} 210 211function HH(a,b,c,d,x,s,ac) { 212 a = a+H(b, c, d) + x + ac; 213 a = rotateLeft(a, s); 214 a = a+b; 215 return a; 216} 217 218function II(a,b,c,d,x,s,ac) { 219 a = a+I(b, c, d) + x + ac; 220 a = rotateLeft(a, s); 221 a = a+b; 222 return a; 223} 224 225function transform(buf,offset) { 226 var a=0, b=0, c=0, d=0; 227 var x = transformBuffer; 228 229 a = state[0]; 230 b = state[1]; 231 c = state[2]; 232 d = state[3]; 233 234 for (i = 0; i < 16; i++) { 235 x[i] = and(buf[i*4+offset],0xff); 236 for (j = 1; j < 4; j++) { 237 x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8); 238 } 239 } 240 241 /* Round 1 */ 242 a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ 243 d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ 244 c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ 245 b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ 246 a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ 247 d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ 248 c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ 249 b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ 250 a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ 251 d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ 252 c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ 253 b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ 254 a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ 255 d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ 256 c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ 257 b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ 258 259 /* Round 2 */ 260 a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ 261 d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ 262 c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ 263 b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ 264 a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ 265 d = GG ( d, a, b, c, x[10], S22, 0x2441453); /* 22 */ 266 c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ 267 b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ 268 a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ 269 d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ 270 c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ 271 b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ 272 a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 273 d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ 274 c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ 275 b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 276 277 /* Round 3 */ 278 a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ 279 d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ 280 c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ 281 b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ 282 a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ 283 d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ 284 c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ 285 b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ 286 a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ 287 d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ 288 c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ 289 b = HH ( b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ 290 a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ 291 d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ 292 c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ 293 b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ 294 295 /* Round 4 */ 296 a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ 297 d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ 298 c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ 299 b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ 300 a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ 301 d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ 302 c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ 303 b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ 304 a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ 305 d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ 306 c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ 307 b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ 308 a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ 309 d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ 310 c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ 311 b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ 312 313 state[0] +=a; 314 state[1] +=b; 315 state[2] +=c; 316 state[3] +=d; 317} 318 319function init() { 320 count[0]=count[1] = 0; 321 state[0] = 0x67452301; 322 state[1] = 0xefcdab89; 323 state[2] = 0x98badcfe; 324 state[3] = 0x10325476; 325 for (i = 0; i < digestBits.length; i++) 326 digestBits[i] = 0; 327} 328 329function update(b) { 330 var index,i; 331 332 index = and(shr(count[0],3) , 0x3f); 333 if (count[0]<0xffffffff-7) 334 count[0] += 8; 335 else { 336 count[1]++; 337 count[0]-=0xffffffff+1; 338 count[0]+=8; 339 } 340 341 buffer[index] = and(b,0xff); 342 if (index >= 63) { 343 transform(buffer, 0); 344 } 345} 346 347function finish() { 348 var bits = new array(8); 349 var padding; 350 var i=0, index=0, padLen=0; 351 352 for (i = 0; i < 4; i++) { 353 bits[i] = and(shr(count[0],(i * 8)), 0xff); 354 } 355 356 for (i = 0; i < 4; i++) { 357 bits[i+4]=and(shr(count[1],(i * 8)), 0xff); 358 } 359 360 index = and(shr(count[0], 3) ,0x3f); 361 padLen = (index < 56) ? (56 - index) : (120 - index); 362 padding = new array(64); 363 padding[0] = 0x80; 364 for (i=0;i<padLen;i++) 365 update(padding[i]); 366 367 for (i=0;i<8;i++) 368 update(bits[i]); 369 370 for (i = 0; i < 4; i++) { 371 for (j = 0; j < 4; j++) { 372 digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff); 373 } 374 } 375} 376 377/* End of the MD5 algorithm */ 378 379function hexa(n) { 380 var hexa_h = "0123456789abcdef"; 381 var hexa_c=""; 382 var hexa_m=n; 383 for (hexa_i=0;hexa_i<8;hexa_i++) { 384 hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c; 385 hexa_m=Math.floor(hexa_m/16); 386 } 387 return hexa_c; 388} 389 390var ascii="01234567890123456789012345678901" + 391" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+ 392"[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; 393 394function MD5(entree) 395{ 396 var l,s,k,ka,kb,kc,kd; 397 398 init(); 399 for (k=0;k<entree.length;k++) { 400 l=entree.charAt(k); 401 update(ascii.lastIndexOf(l)); 402 } 403 finish(); 404 ka=kb=kc=kd=0; 405 for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8)); 406 for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8)); 407 for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8)); 408 for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8)); 409 s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka); 410 return s; 411} 412 413shouldBe("MD5('kde')", "'186cf28b76f2264e9fea8fcf91cb4f5d'"); 414