Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | // |
| 28 | // A JavaScript implementation of the RSA Data Security, Inc. MD5 Message |
| 29 | // Digest Algorithm, as defined in RFC 1321. |
| 30 | // Version 2.1 Copyright (C) Paul Johnston 1999 - 2002. |
| 31 | // Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet |
| 32 | // Distributed under the BSD License |
| 33 | // See http://pajhome.org.uk/crypt/md5 for more info. |
| 34 | // |
| 35 | |
| 36 | function hex_md5(s) { |
| 37 | return binl2hex(core_md5(str2binl(s), s.length * 8)); |
| 38 | } |
| 39 | |
| 40 | function core_md5(x, len) { |
| 41 | /* append padding */ |
| 42 | x[len >> 5] |= 0x80 << ((len) % 32); |
| 43 | x[(((len + 64) >>> 9) << 4) + 14] = len; |
| 44 | |
| 45 | var a = 1732584193; |
| 46 | var b = -271733879; |
| 47 | var c = -1732584194; |
| 48 | var d = 271733878; |
| 49 | |
| 50 | for (var i = 0; i < x.length; i += 16) { |
| 51 | var olda = a; |
| 52 | var oldb = b; |
| 53 | var oldc = c; |
| 54 | var oldd = d; |
| 55 | |
| 56 | a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936); |
| 57 | d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586); |
| 58 | c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819); |
| 59 | b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330); |
| 60 | a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897); |
| 61 | d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426); |
| 62 | c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341); |
| 63 | b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983); |
| 64 | a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416); |
| 65 | d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417); |
| 66 | c = md5_ff(c, d, a, b, x[i+10], 17, -42063); |
| 67 | b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162); |
| 68 | a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682); |
| 69 | d = md5_ff(d, a, b, c, x[i+13], 12, -40341101); |
| 70 | c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290); |
| 71 | b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329); |
| 72 | |
| 73 | a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510); |
| 74 | d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632); |
| 75 | c = md5_gg(c, d, a, b, x[i+11], 14, 643717713); |
| 76 | b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302); |
| 77 | a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691); |
| 78 | d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083); |
| 79 | c = md5_gg(c, d, a, b, x[i+15], 14, -660478335); |
| 80 | b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848); |
| 81 | a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438); |
| 82 | d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690); |
| 83 | c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961); |
| 84 | b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501); |
| 85 | a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467); |
| 86 | d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784); |
| 87 | c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473); |
| 88 | b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734); |
| 89 | |
| 90 | a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558); |
| 91 | d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463); |
| 92 | c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562); |
| 93 | b = md5_hh(b, c, d, a, x[i+14], 23, -35309556); |
| 94 | a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060); |
| 95 | d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353); |
| 96 | c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632); |
| 97 | b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640); |
| 98 | a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174); |
| 99 | d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222); |
| 100 | c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979); |
| 101 | b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189); |
| 102 | a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487); |
| 103 | d = md5_hh(d, a, b, c, x[i+12], 11, -421815835); |
| 104 | c = md5_hh(c, d, a, b, x[i+15], 16, 530742520); |
| 105 | b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651); |
| 106 | |
| 107 | a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844); |
| 108 | d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415); |
| 109 | c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905); |
| 110 | b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055); |
| 111 | a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571); |
| 112 | d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606); |
| 113 | c = md5_ii(c, d, a, b, x[i+10], 15, -1051523); |
| 114 | b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799); |
| 115 | a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359); |
| 116 | d = md5_ii(d, a, b, c, x[i+15], 10, -30611744); |
| 117 | c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380); |
| 118 | b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649); |
| 119 | a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070); |
| 120 | d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379); |
| 121 | c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259); |
| 122 | b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551); |
| 123 | |
| 124 | a = safe_add(a, olda); |
| 125 | b = safe_add(b, oldb); |
| 126 | c = safe_add(c, oldc); |
| 127 | d = safe_add(d, oldd); |
| 128 | } |
| 129 | return Array(a, b, c, d); |
| 130 | } |
| 131 | |
| 132 | function md5_cmn(q, a, b, x, s, t) { |
| 133 | return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b); |
| 134 | } |
| 135 | |
| 136 | function md5_ff(a, b, c, d, x, s, t) { |
| 137 | return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t); |
| 138 | } |
| 139 | |
| 140 | function md5_gg(a, b, c, d, x, s, t) { |
| 141 | return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t); |
| 142 | } |
| 143 | |
| 144 | function md5_hh(a, b, c, d, x, s, t) { |
| 145 | return md5_cmn(b ^ c ^ d, a, b, x, s, t); |
| 146 | } |
| 147 | |
| 148 | function md5_ii(a, b, c, d, x, s, t) { |
| 149 | return md5_cmn(c ^ (b | (~d)), a, b, x, s, t); |
| 150 | } |
| 151 | |
| 152 | function safe_add(x, y) { |
| 153 | var lsw = (x & 0xFFFF) + (y & 0xFFFF); |
| 154 | var msw = (x >> 16) + (y >> 16) + (lsw >> 16); |
| 155 | return (msw << 16) | (lsw & 0xFFFF); |
| 156 | } |
| 157 | |
| 158 | function bit_rol(num, cnt) { |
| 159 | return (num << cnt) | (num >>> (32 - cnt)); |
| 160 | } |
| 161 | |
| 162 | function str2binl(str) { |
| 163 | var bin = Array(); |
| 164 | var mask = (1 << 8) - 1; |
| 165 | for(var i = 0; i < str.length * 8; i += 8) |
| 166 | bin[i>>5] |= (str.charCodeAt(i / 8) & mask) << (i%32); |
| 167 | return bin; |
| 168 | } |
| 169 | |
| 170 | function binl2hex(binarray) { |
| 171 | var hex_tab = "0123456789abcdef"; |
| 172 | var str = ""; |
| 173 | for(var i = 0; i < binarray.length * 4; i++) { |
| 174 | str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) + |
| 175 | hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF); |
| 176 | } |
| 177 | return str; |
| 178 | } |
| 179 | |
| 180 | var plainText = "Rebellious subjects, enemies to peace,\n\ |
| 181 | Profaners of this neighbour-stained steel,--\n\ |
| 182 | Will they not hear? What, ho! you men, you beasts,\n\ |
| 183 | That quench the fire of your pernicious rage\n\ |
| 184 | With purple fountains issuing from your veins,\n\ |
| 185 | On pain of torture, from those bloody hands\n\ |
| 186 | Throw your mistemper'd weapons to the ground,\n\ |
| 187 | And hear the sentence of your moved prince.\n\ |
| 188 | Three civil brawls, bred of an airy word,\n\ |
| 189 | By thee, old Capulet, and Montague,\n\ |
| 190 | Have thrice disturb'd the quiet of our streets,\n\ |
| 191 | And made Verona's ancient citizens\n\ |
| 192 | Cast by their grave beseeming ornaments,\n\ |
| 193 | To wield old partisans, in hands as old,\n\ |
| 194 | Canker'd with peace, to part your canker'd hate:\n\ |
| 195 | If ever you disturb our streets again,\n\ |
| 196 | Your lives shall pay the forfeit of the peace.\n\ |
| 197 | For this time, all the rest depart away:\n\ |
| 198 | You Capulet; shall go along with me:\n\ |
| 199 | And, Montague, come you this afternoon,\n\ |
| 200 | To know our further pleasure in this case,\n\ |
| 201 | To old Free-town, our common judgment-place.\n\ |
| 202 | Once more, on pain of death, all men depart.\n" |
| 203 | |
| 204 | for (var i = 0; i < 4; ++i) { |
| 205 | plainText += plainText; |
| 206 | } |
| 207 | |
| 208 | assertEquals(hex_md5("abc"), "900150983cd24fb0d6963f7d28e17f72"); |
| 209 | for (var i = 0; i < 11; ++i) { |
| 210 | assertEquals(hex_md5(plainText), "1b8719c72d5d8bfd06e096ef6c6288c5"); |
| 211 | } |