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