File indexing completed on 2024-04-28 15:28:38
0001 shouldBe("hexMD5('kde')", "'186cf28b76f2264e9fea8fcf91cb4f5d'"); 0002 0003 /* 0004 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 0005 * Digest Algorithm, as defined in RFC 1321. 0006 * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. 0007 * Code also contributed by Greg Holt 0008 * See http://pajhome.org.uk/site/legal.html for details. 0009 */ 0010 0011 /* 0012 * Add integers, wrapping at 2^32. This uses 16-bit operations internally 0013 * to work around bugs in some JS interpreters. 0014 */ 0015 function safe_add(x, y) 0016 { 0017 var lsw = (x & 0xFFFF) + (y & 0xFFFF) 0018 var msw = (x >> 16) + (y >> 16) + (lsw >> 16) 0019 return (msw << 16) | (lsw & 0xFFFF) 0020 } 0021 0022 /* 0023 * Bitwise rotate a 32-bit number to the left. 0024 */ 0025 function rol(num, cnt) 0026 { 0027 return (num << cnt) | (num >>> (32 - cnt)) 0028 } 0029 0030 /* 0031 * These functions implement the four basic operations the algorithm uses. 0032 */ 0033 function cmn(q, a, b, x, s, t) 0034 { 0035 return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b) 0036 } 0037 function ff(a, b, c, d, x, s, t) 0038 { 0039 return cmn((b & c) | ((~b) & d), a, b, x, s, t) 0040 } 0041 function gg(a, b, c, d, x, s, t) 0042 { 0043 return cmn((b & d) | (c & (~d)), a, b, x, s, t) 0044 } 0045 function hh(a, b, c, d, x, s, t) 0046 { 0047 return cmn(b ^ c ^ d, a, b, x, s, t) 0048 } 0049 function ii(a, b, c, d, x, s, t) 0050 { 0051 return cmn(c ^ (b | (~d)), a, b, x, s, t) 0052 } 0053 0054 /* 0055 * Calculate the MD5 of an array of little-endian words, producing an array 0056 * of little-endian words. 0057 */ 0058 function coreMD5(x) 0059 { 0060 var a = 1732584193 0061 var b = -271733879 0062 var c = -1732584194 0063 var d = 271733878 0064 0065 for(i = 0; i < x.length; i += 16) 0066 { 0067 var olda = a 0068 var oldb = b 0069 var oldc = c 0070 var oldd = d 0071 0072 a = ff(a, b, c, d, x[i+ 0], 7 , -680876936) 0073 d = ff(d, a, b, c, x[i+ 1], 12, -389564586) 0074 c = ff(c, d, a, b, x[i+ 2], 17, 606105819) 0075 b = ff(b, c, d, a, x[i+ 3], 22, -1044525330) 0076 a = ff(a, b, c, d, x[i+ 4], 7 , -176418897) 0077 d = ff(d, a, b, c, x[i+ 5], 12, 1200080426) 0078 c = ff(c, d, a, b, x[i+ 6], 17, -1473231341) 0079 b = ff(b, c, d, a, x[i+ 7], 22, -45705983) 0080 a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416) 0081 d = ff(d, a, b, c, x[i+ 9], 12, -1958414417) 0082 c = ff(c, d, a, b, x[i+10], 17, -42063) 0083 b = ff(b, c, d, a, x[i+11], 22, -1990404162) 0084 a = ff(a, b, c, d, x[i+12], 7 , 1804603682) 0085 d = ff(d, a, b, c, x[i+13], 12, -40341101) 0086 c = ff(c, d, a, b, x[i+14], 17, -1502002290) 0087 b = ff(b, c, d, a, x[i+15], 22, 1236535329) 0088 0089 a = gg(a, b, c, d, x[i+ 1], 5 , -165796510) 0090 d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632) 0091 c = gg(c, d, a, b, x[i+11], 14, 643717713) 0092 b = gg(b, c, d, a, x[i+ 0], 20, -373897302) 0093 a = gg(a, b, c, d, x[i+ 5], 5 , -701558691) 0094 d = gg(d, a, b, c, x[i+10], 9 , 38016083) 0095 c = gg(c, d, a, b, x[i+15], 14, -660478335) 0096 b = gg(b, c, d, a, x[i+ 4], 20, -405537848) 0097 a = gg(a, b, c, d, x[i+ 9], 5 , 568446438) 0098 d = gg(d, a, b, c, x[i+14], 9 , -1019803690) 0099 c = gg(c, d, a, b, x[i+ 3], 14, -187363961) 0100 b = gg(b, c, d, a, x[i+ 8], 20, 1163531501) 0101 a = gg(a, b, c, d, x[i+13], 5 , -1444681467) 0102 d = gg(d, a, b, c, x[i+ 2], 9 , -51403784) 0103 c = gg(c, d, a, b, x[i+ 7], 14, 1735328473) 0104 b = gg(b, c, d, a, x[i+12], 20, -1926607734) 0105 0106 a = hh(a, b, c, d, x[i+ 5], 4 , -378558) 0107 d = hh(d, a, b, c, x[i+ 8], 11, -2022574463) 0108 c = hh(c, d, a, b, x[i+11], 16, 1839030562) 0109 b = hh(b, c, d, a, x[i+14], 23, -35309556) 0110 a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060) 0111 d = hh(d, a, b, c, x[i+ 4], 11, 1272893353) 0112 c = hh(c, d, a, b, x[i+ 7], 16, -155497632) 0113 b = hh(b, c, d, a, x[i+10], 23, -1094730640) 0114 a = hh(a, b, c, d, x[i+13], 4 , 681279174) 0115 d = hh(d, a, b, c, x[i+ 0], 11, -358537222) 0116 c = hh(c, d, a, b, x[i+ 3], 16, -722521979) 0117 b = hh(b, c, d, a, x[i+ 6], 23, 76029189) 0118 a = hh(a, b, c, d, x[i+ 9], 4 , -640364487) 0119 d = hh(d, a, b, c, x[i+12], 11, -421815835) 0120 c = hh(c, d, a, b, x[i+15], 16, 530742520) 0121 b = hh(b, c, d, a, x[i+ 2], 23, -995338651) 0122 0123 a = ii(a, b, c, d, x[i+ 0], 6 , -198630844) 0124 d = ii(d, a, b, c, x[i+ 7], 10, 1126891415) 0125 c = ii(c, d, a, b, x[i+14], 15, -1416354905) 0126 b = ii(b, c, d, a, x[i+ 5], 21, -57434055) 0127 a = ii(a, b, c, d, x[i+12], 6 , 1700485571) 0128 d = ii(d, a, b, c, x[i+ 3], 10, -1894986606) 0129 c = ii(c, d, a, b, x[i+10], 15, -1051523) 0130 b = ii(b, c, d, a, x[i+ 1], 21, -2054922799) 0131 a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359) 0132 d = ii(d, a, b, c, x[i+15], 10, -30611744) 0133 c = ii(c, d, a, b, x[i+ 6], 15, -1560198380) 0134 b = ii(b, c, d, a, x[i+13], 21, 1309151649) 0135 a = ii(a, b, c, d, x[i+ 4], 6 , -145523070) 0136 d = ii(d, a, b, c, x[i+11], 10, -1120210379) 0137 c = ii(c, d, a, b, x[i+ 2], 15, 718787259) 0138 b = ii(b, c, d, a, x[i+ 9], 21, -343485551) 0139 0140 a = safe_add(a, olda) 0141 b = safe_add(b, oldb) 0142 c = safe_add(c, oldc) 0143 d = safe_add(d, oldd) 0144 } 0145 return [a, b, c, d] 0146 } 0147 0148 /* 0149 * Convert an array of little-endian words to a hex string. 0150 */ 0151 function binl2hex(binarray) 0152 { 0153 var hex_tab = "0123456789abcdef" 0154 var str = "" 0155 for(var i = 0; i < binarray.length * 4; i++) 0156 { 0157 str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + 0158 hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF) 0159 } 0160 return str 0161 } 0162 0163 /* 0164 * Convert an array of little-endian words to a base64 encoded string. 0165 */ 0166 function binl2b64(binarray) 0167 { 0168 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 0169 var str = "" 0170 for(var i = 0; i < binarray.length * 32; i += 6) 0171 { 0172 str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) | 0173 ((binarray[i>>5+1] >> (32-i%32)) & 0x3F)) 0174 } 0175 return str 0176 } 0177 0178 /* 0179 * Convert an 8-bit character string to a sequence of 16-word blocks, stored 0180 * as an array, and append appropriate padding for MD4/5 calculation. 0181 * If any of the characters are >255, the high byte is silently ignored. 0182 */ 0183 function str2binl(str) 0184 { 0185 var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks 0186 var blks = new Array(nblk * 16) 0187 for(var i = 0; i < nblk * 16; i++) blks[i] = 0 0188 for(var i = 0; i < str.length; i++) 0189 blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8) 0190 blks[i>>2] |= 0x80 << ((i%4) * 8) 0191 blks[nblk*16-2] = str.length * 8 0192 return blks 0193 } 0194 0195 /* 0196 * Convert a wide-character string to a sequence of 16-word blocks, stored as 0197 * an array, and append appropriate padding for MD4/5 calculation. 0198 */ 0199 function strw2binl(str) 0200 { 0201 var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks 0202 var blks = new Array(nblk * 16) 0203 for(var i = 0; i < nblk * 16; i++) blks[i] = 0 0204 for(var i = 0; i < str.length; i++) 0205 blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16) 0206 blks[i>>1] |= 0x80 << ((i%2) * 16) 0207 blks[nblk*16-2] = str.length * 16 0208 return blks 0209 } 0210 0211 /* 0212 * External interface 0213 */ 0214 function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) } 0215 function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) } 0216 function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) } 0217 function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) } 0218 /* Backward compatibility */ 0219 function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) } 0220