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"); 25shouldBe("hexMD5('kde')", "'186cf28b76f2264e9fea8fcf91cb4f5d'"); 26 27/* 28 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 29 * Digest Algorithm, as defined in RFC 1321. 30 * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. 31 * Code also contributed by Greg Holt 32 * See http://pajhome.org.uk/site/legal.html for details. 33 */ 34 35/* 36 * Add integers, wrapping at 2^32. This uses 16-bit operations internally 37 * to work around bugs in some JS interpreters. 38 */ 39function safe_add(x, y) 40{ 41 var lsw = (x & 0xFFFF) + (y & 0xFFFF) 42 var msw = (x >> 16) + (y >> 16) + (lsw >> 16) 43 return (msw << 16) | (lsw & 0xFFFF) 44} 45 46/* 47 * Bitwise rotate a 32-bit number to the left. 48 */ 49function rol(num, cnt) 50{ 51 return (num << cnt) | (num >>> (32 - cnt)) 52} 53 54/* 55 * These functions implement the four basic operations the algorithm uses. 56 */ 57function cmn(q, a, b, x, s, t) 58{ 59 return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b) 60} 61function ff(a, b, c, d, x, s, t) 62{ 63 return cmn((b & c) | ((~b) & d), a, b, x, s, t) 64} 65function gg(a, b, c, d, x, s, t) 66{ 67 return cmn((b & d) | (c & (~d)), a, b, x, s, t) 68} 69function hh(a, b, c, d, x, s, t) 70{ 71 return cmn(b ^ c ^ d, a, b, x, s, t) 72} 73function ii(a, b, c, d, x, s, t) 74{ 75 return cmn(c ^ (b | (~d)), a, b, x, s, t) 76} 77 78/* 79 * Calculate the MD5 of an array of little-endian words, producing an array 80 * of little-endian words. 81 */ 82function coreMD5(x) 83{ 84 var a = 1732584193 85 var b = -271733879 86 var c = -1732584194 87 var d = 271733878 88 89 for(i = 0; i < x.length; i += 16) 90 { 91 var olda = a 92 var oldb = b 93 var oldc = c 94 var oldd = d 95 96 a = ff(a, b, c, d, x[i+ 0], 7 , -680876936) 97 d = ff(d, a, b, c, x[i+ 1], 12, -389564586) 98 c = ff(c, d, a, b, x[i+ 2], 17, 606105819) 99 b = ff(b, c, d, a, x[i+ 3], 22, -1044525330) 100 a = ff(a, b, c, d, x[i+ 4], 7 , -176418897) 101 d = ff(d, a, b, c, x[i+ 5], 12, 1200080426) 102 c = ff(c, d, a, b, x[i+ 6], 17, -1473231341) 103 b = ff(b, c, d, a, x[i+ 7], 22, -45705983) 104 a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416) 105 d = ff(d, a, b, c, x[i+ 9], 12, -1958414417) 106 c = ff(c, d, a, b, x[i+10], 17, -42063) 107 b = ff(b, c, d, a, x[i+11], 22, -1990404162) 108 a = ff(a, b, c, d, x[i+12], 7 , 1804603682) 109 d = ff(d, a, b, c, x[i+13], 12, -40341101) 110 c = ff(c, d, a, b, x[i+14], 17, -1502002290) 111 b = ff(b, c, d, a, x[i+15], 22, 1236535329) 112 113 a = gg(a, b, c, d, x[i+ 1], 5 , -165796510) 114 d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632) 115 c = gg(c, d, a, b, x[i+11], 14, 643717713) 116 b = gg(b, c, d, a, x[i+ 0], 20, -373897302) 117 a = gg(a, b, c, d, x[i+ 5], 5 , -701558691) 118 d = gg(d, a, b, c, x[i+10], 9 , 38016083) 119 c = gg(c, d, a, b, x[i+15], 14, -660478335) 120 b = gg(b, c, d, a, x[i+ 4], 20, -405537848) 121 a = gg(a, b, c, d, x[i+ 9], 5 , 568446438) 122 d = gg(d, a, b, c, x[i+14], 9 , -1019803690) 123 c = gg(c, d, a, b, x[i+ 3], 14, -187363961) 124 b = gg(b, c, d, a, x[i+ 8], 20, 1163531501) 125 a = gg(a, b, c, d, x[i+13], 5 , -1444681467) 126 d = gg(d, a, b, c, x[i+ 2], 9 , -51403784) 127 c = gg(c, d, a, b, x[i+ 7], 14, 1735328473) 128 b = gg(b, c, d, a, x[i+12], 20, -1926607734) 129 130 a = hh(a, b, c, d, x[i+ 5], 4 , -378558) 131 d = hh(d, a, b, c, x[i+ 8], 11, -2022574463) 132 c = hh(c, d, a, b, x[i+11], 16, 1839030562) 133 b = hh(b, c, d, a, x[i+14], 23, -35309556) 134 a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060) 135 d = hh(d, a, b, c, x[i+ 4], 11, 1272893353) 136 c = hh(c, d, a, b, x[i+ 7], 16, -155497632) 137 b = hh(b, c, d, a, x[i+10], 23, -1094730640) 138 a = hh(a, b, c, d, x[i+13], 4 , 681279174) 139 d = hh(d, a, b, c, x[i+ 0], 11, -358537222) 140 c = hh(c, d, a, b, x[i+ 3], 16, -722521979) 141 b = hh(b, c, d, a, x[i+ 6], 23, 76029189) 142 a = hh(a, b, c, d, x[i+ 9], 4 , -640364487) 143 d = hh(d, a, b, c, x[i+12], 11, -421815835) 144 c = hh(c, d, a, b, x[i+15], 16, 530742520) 145 b = hh(b, c, d, a, x[i+ 2], 23, -995338651) 146 147 a = ii(a, b, c, d, x[i+ 0], 6 , -198630844) 148 d = ii(d, a, b, c, x[i+ 7], 10, 1126891415) 149 c = ii(c, d, a, b, x[i+14], 15, -1416354905) 150 b = ii(b, c, d, a, x[i+ 5], 21, -57434055) 151 a = ii(a, b, c, d, x[i+12], 6 , 1700485571) 152 d = ii(d, a, b, c, x[i+ 3], 10, -1894986606) 153 c = ii(c, d, a, b, x[i+10], 15, -1051523) 154 b = ii(b, c, d, a, x[i+ 1], 21, -2054922799) 155 a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359) 156 d = ii(d, a, b, c, x[i+15], 10, -30611744) 157 c = ii(c, d, a, b, x[i+ 6], 15, -1560198380) 158 b = ii(b, c, d, a, x[i+13], 21, 1309151649) 159 a = ii(a, b, c, d, x[i+ 4], 6 , -145523070) 160 d = ii(d, a, b, c, x[i+11], 10, -1120210379) 161 c = ii(c, d, a, b, x[i+ 2], 15, 718787259) 162 b = ii(b, c, d, a, x[i+ 9], 21, -343485551) 163 164 a = safe_add(a, olda) 165 b = safe_add(b, oldb) 166 c = safe_add(c, oldc) 167 d = safe_add(d, oldd) 168 } 169 return [a, b, c, d] 170} 171 172/* 173 * Convert an array of little-endian words to a hex string. 174 */ 175function binl2hex(binarray) 176{ 177 var hex_tab = "0123456789abcdef" 178 var str = "" 179 for(var i = 0; i < binarray.length * 4; i++) 180 { 181 str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + 182 hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF) 183 } 184 return str 185} 186 187/* 188 * Convert an array of little-endian words to a base64 encoded string. 189 */ 190function binl2b64(binarray) 191{ 192 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 193 var str = "" 194 for(var i = 0; i < binarray.length * 32; i += 6) 195 { 196 str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) | 197 ((binarray[i>>5+1] >> (32-i%32)) & 0x3F)) 198 } 199 return str 200} 201 202/* 203 * Convert an 8-bit character string to a sequence of 16-word blocks, stored 204 * as an array, and append appropriate padding for MD4/5 calculation. 205 * If any of the characters are >255, the high byte is silently ignored. 206 */ 207function str2binl(str) 208{ 209 var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks 210 var blks = new Array(nblk * 16) 211 for(var i = 0; i < nblk * 16; i++) blks[i] = 0 212 for(var i = 0; i < str.length; i++) 213 blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8) 214 blks[i>>2] |= 0x80 << ((i%4) * 8) 215 blks[nblk*16-2] = str.length * 8 216 return blks 217} 218 219/* 220 * Convert a wide-character string to a sequence of 16-word blocks, stored as 221 * an array, and append appropriate padding for MD4/5 calculation. 222 */ 223function strw2binl(str) 224{ 225 var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks 226 var blks = new Array(nblk * 16) 227 for(var i = 0; i < nblk * 16; i++) blks[i] = 0 228 for(var i = 0; i < str.length; i++) 229 blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16) 230 blks[i>>1] |= 0x80 << ((i%2) * 16) 231 blks[nblk*16-2] = str.length * 16 232 return blks 233} 234 235/* 236 * External interface 237 */ 238function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) } 239function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) } 240function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) } 241function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) } 242/* Backward compatibility */ 243function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) } 244