src/tizen/hashes/md5.js

Wed, 31 Jul 2013 19:48:00 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Jul 2013 19:48:00 +0200
changeset 14
d1b294812560
permissions
-rw-r--r--

Introduce port to the Tizen OS.

michael@14 1 /*
michael@14 2 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
michael@14 3 * Digest Algorithm, as defined in RFC 1321.
michael@14 4 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
michael@14 5 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
michael@14 6 * Distributed under the BSD License
michael@14 7 * See http://pajhome.org.uk/crypt/md5 for more info.
michael@14 8 */
michael@14 9
michael@14 10 /*
michael@14 11 * Configurable variables. You may need to tweak these to be compatible with
michael@14 12 * the server-side, but the defaults work in most cases.
michael@14 13 */
michael@14 14 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
michael@14 15 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
michael@14 16 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
michael@14 17
michael@14 18 /*
michael@14 19 * These are the functions you'll usually want to call
michael@14 20 * They take string arguments and return either hex or base-64 encoded strings
michael@14 21 */
michael@14 22 function hexmd5(s){ return binl2hex(coremd5(str2binl(s), s.length * chrsz));}
michael@14 23 function b64md5(s){ return binl2b64(coremd5(str2binl(s), s.length * chrsz));}
michael@14 24 function strmd5(s){ return binl2str(coremd5(str2binl(s), s.length * chrsz));}
michael@14 25 function hexhmacmd5(key, data) { return binl2hex(corehmacmd5(key, data)); }
michael@14 26 function b64hmacmd5(key, data) { return binl2b64(corehmacmd5(key, data)); }
michael@14 27 function strhmacmd5(key, data) { return binl2str(corehmacmd5(key, data)); }
michael@14 28
michael@14 29 /*
michael@14 30 * Perform a simple self-test to see if the VM is working
michael@14 31 */
michael@14 32 function md5vmtest()
michael@14 33 {
michael@14 34 return hexmd5("abc") == "900150983cd24fb0d6963f7d28e17f72";
michael@14 35 }
michael@14 36
michael@14 37 /*
michael@14 38 * Calculate the MD5 of an array of little-endian words, and a bit length
michael@14 39 */
michael@14 40 function coremd5(x, len)
michael@14 41 {
michael@14 42 /* append padding */
michael@14 43 x[len >> 5] |= 0x80 << ((len) % 32);
michael@14 44 x[(((len + 64) >>> 9) << 4) + 14] = len;
michael@14 45
michael@14 46 var a = 1732584193;
michael@14 47 var b = -271733879;
michael@14 48 var c = -1732584194;
michael@14 49 var d = 271733878;
michael@14 50
michael@14 51 for(var i = 0; i < x.length; i += 16)
michael@14 52 {
michael@14 53 var olda = a;
michael@14 54 var oldb = b;
michael@14 55 var oldc = c;
michael@14 56 var oldd = d;
michael@14 57
michael@14 58 a = md5ff(a, b, c, d, x[i+ 0], 7 , -680876936);
michael@14 59 d = md5ff(d, a, b, c, x[i+ 1], 12, -389564586);
michael@14 60 c = md5ff(c, d, a, b, x[i+ 2], 17, 606105819);
michael@14 61 b = md5ff(b, c, d, a, x[i+ 3], 22, -1044525330);
michael@14 62 a = md5ff(a, b, c, d, x[i+ 4], 7 , -176418897);
michael@14 63 d = md5ff(d, a, b, c, x[i+ 5], 12, 1200080426);
michael@14 64 c = md5ff(c, d, a, b, x[i+ 6], 17, -1473231341);
michael@14 65 b = md5ff(b, c, d, a, x[i+ 7], 22, -45705983);
michael@14 66 a = md5ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
michael@14 67 d = md5ff(d, a, b, c, x[i+ 9], 12, -1958414417);
michael@14 68 c = md5ff(c, d, a, b, x[i+10], 17, -42063);
michael@14 69 b = md5ff(b, c, d, a, x[i+11], 22, -1990404162);
michael@14 70 a = md5ff(a, b, c, d, x[i+12], 7 , 1804603682);
michael@14 71 d = md5ff(d, a, b, c, x[i+13], 12, -40341101);
michael@14 72 c = md5ff(c, d, a, b, x[i+14], 17, -1502002290);
michael@14 73 b = md5ff(b, c, d, a, x[i+15], 22, 1236535329);
michael@14 74
michael@14 75 a = md5gg(a, b, c, d, x[i+ 1], 5 , -165796510);
michael@14 76 d = md5gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
michael@14 77 c = md5gg(c, d, a, b, x[i+11], 14, 643717713);
michael@14 78 b = md5gg(b, c, d, a, x[i+ 0], 20, -373897302);
michael@14 79 a = md5gg(a, b, c, d, x[i+ 5], 5 , -701558691);
michael@14 80 d = md5gg(d, a, b, c, x[i+10], 9 , 38016083);
michael@14 81 c = md5gg(c, d, a, b, x[i+15], 14, -660478335);
michael@14 82 b = md5gg(b, c, d, a, x[i+ 4], 20, -405537848);
michael@14 83 a = md5gg(a, b, c, d, x[i+ 9], 5 , 568446438);
michael@14 84 d = md5gg(d, a, b, c, x[i+14], 9 , -1019803690);
michael@14 85 c = md5gg(c, d, a, b, x[i+ 3], 14, -187363961);
michael@14 86 b = md5gg(b, c, d, a, x[i+ 8], 20, 1163531501);
michael@14 87 a = md5gg(a, b, c, d, x[i+13], 5 , -1444681467);
michael@14 88 d = md5gg(d, a, b, c, x[i+ 2], 9 , -51403784);
michael@14 89 c = md5gg(c, d, a, b, x[i+ 7], 14, 1735328473);
michael@14 90 b = md5gg(b, c, d, a, x[i+12], 20, -1926607734);
michael@14 91
michael@14 92 a = md5hh(a, b, c, d, x[i+ 5], 4 , -378558);
michael@14 93 d = md5hh(d, a, b, c, x[i+ 8], 11, -2022574463);
michael@14 94 c = md5hh(c, d, a, b, x[i+11], 16, 1839030562);
michael@14 95 b = md5hh(b, c, d, a, x[i+14], 23, -35309556);
michael@14 96 a = md5hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
michael@14 97 d = md5hh(d, a, b, c, x[i+ 4], 11, 1272893353);
michael@14 98 c = md5hh(c, d, a, b, x[i+ 7], 16, -155497632);
michael@14 99 b = md5hh(b, c, d, a, x[i+10], 23, -1094730640);
michael@14 100 a = md5hh(a, b, c, d, x[i+13], 4 , 681279174);
michael@14 101 d = md5hh(d, a, b, c, x[i+ 0], 11, -358537222);
michael@14 102 c = md5hh(c, d, a, b, x[i+ 3], 16, -722521979);
michael@14 103 b = md5hh(b, c, d, a, x[i+ 6], 23, 76029189);
michael@14 104 a = md5hh(a, b, c, d, x[i+ 9], 4 , -640364487);
michael@14 105 d = md5hh(d, a, b, c, x[i+12], 11, -421815835);
michael@14 106 c = md5hh(c, d, a, b, x[i+15], 16, 530742520);
michael@14 107 b = md5hh(b, c, d, a, x[i+ 2], 23, -995338651);
michael@14 108
michael@14 109 a = md5ii(a, b, c, d, x[i+ 0], 6 , -198630844);
michael@14 110 d = md5ii(d, a, b, c, x[i+ 7], 10, 1126891415);
michael@14 111 c = md5ii(c, d, a, b, x[i+14], 15, -1416354905);
michael@14 112 b = md5ii(b, c, d, a, x[i+ 5], 21, -57434055);
michael@14 113 a = md5ii(a, b, c, d, x[i+12], 6 , 1700485571);
michael@14 114 d = md5ii(d, a, b, c, x[i+ 3], 10, -1894986606);
michael@14 115 c = md5ii(c, d, a, b, x[i+10], 15, -1051523);
michael@14 116 b = md5ii(b, c, d, a, x[i+ 1], 21, -2054922799);
michael@14 117 a = md5ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
michael@14 118 d = md5ii(d, a, b, c, x[i+15], 10, -30611744);
michael@14 119 c = md5ii(c, d, a, b, x[i+ 6], 15, -1560198380);
michael@14 120 b = md5ii(b, c, d, a, x[i+13], 21, 1309151649);
michael@14 121 a = md5ii(a, b, c, d, x[i+ 4], 6 , -145523070);
michael@14 122 d = md5ii(d, a, b, c, x[i+11], 10, -1120210379);
michael@14 123 c = md5ii(c, d, a, b, x[i+ 2], 15, 718787259);
michael@14 124 b = md5ii(b, c, d, a, x[i+ 9], 21, -343485551);
michael@14 125
michael@14 126 a = safeadd(a, olda);
michael@14 127 b = safeadd(b, oldb);
michael@14 128 c = safeadd(c, oldc);
michael@14 129 d = safeadd(d, oldd);
michael@14 130 }
michael@14 131 return Array(a, b, c, d);
michael@14 132
michael@14 133 }
michael@14 134
michael@14 135 /*
michael@14 136 * These functions implement the four basic operations the algorithm uses.
michael@14 137 */
michael@14 138 function md5cmn(q, a, b, x, s, t)
michael@14 139 {
michael@14 140 return safeadd(bitrol(safeadd(safeadd(a, q), safeadd(x, t)), s),b);
michael@14 141 }
michael@14 142 function md5ff(a, b, c, d, x, s, t)
michael@14 143 {
michael@14 144 return md5cmn((b & c) | ((~b) & d), a, b, x, s, t);
michael@14 145 }
michael@14 146 function md5gg(a, b, c, d, x, s, t)
michael@14 147 {
michael@14 148 return md5cmn((b & d) | (c & (~d)), a, b, x, s, t);
michael@14 149 }
michael@14 150 function md5hh(a, b, c, d, x, s, t)
michael@14 151 {
michael@14 152 return md5cmn(b ^ c ^ d, a, b, x, s, t);
michael@14 153 }
michael@14 154 function md5ii(a, b, c, d, x, s, t)
michael@14 155 {
michael@14 156 return md5cmn(c ^ (b | (~d)), a, b, x, s, t);
michael@14 157 }
michael@14 158
michael@14 159 /*
michael@14 160 * Calculate the HMAC-MD5, of a key and some data
michael@14 161 */
michael@14 162 function corehmacmd5(key, data)
michael@14 163 {
michael@14 164 var bkey = str2binl(key);
michael@14 165 if(bkey.length > 16) bkey = coremd5(bkey, key.length * chrsz);
michael@14 166
michael@14 167 var ipad = Array(16), opad = Array(16);
michael@14 168 for(var i = 0; i < 16; i++)
michael@14 169 {
michael@14 170 ipad[i] = bkey[i] ^ 0x36363636;
michael@14 171 opad[i] = bkey[i] ^ 0x5C5C5C5C;
michael@14 172 }
michael@14 173
michael@14 174 var hash = coremd5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
michael@14 175 return coremd5(opad.concat(hash), 512 + 128);
michael@14 176 }
michael@14 177
michael@14 178 /*
michael@14 179 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
michael@14 180 * to work around bugs in some JS interpreters.
michael@14 181 */
michael@14 182 function safeadd(x, y)
michael@14 183 {
michael@14 184 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
michael@14 185 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
michael@14 186 return (msw << 16) | (lsw & 0xFFFF);
michael@14 187 }
michael@14 188
michael@14 189 /*
michael@14 190 * Bitwise rotate a 32-bit number to the left.
michael@14 191 */
michael@14 192 function bitrol(num, cnt)
michael@14 193 {
michael@14 194 return (num << cnt) | (num >>> (32 - cnt));
michael@14 195 }
michael@14 196
michael@14 197 /*
michael@14 198 * Convert a string to an array of little-endian words
michael@14 199 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
michael@14 200 */
michael@14 201 function str2binl(str)
michael@14 202 {
michael@14 203 var bin = Array();
michael@14 204 var mask = (1 << chrsz) - 1;
michael@14 205 for(var i = 0; i < str.length * chrsz; i += chrsz)
michael@14 206 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
michael@14 207 return bin;
michael@14 208 }
michael@14 209
michael@14 210 /*
michael@14 211 * Convert an array of little-endian words to a string
michael@14 212 */
michael@14 213 function binl2str(bin)
michael@14 214 {
michael@14 215 var str = "";
michael@14 216 var mask = (1 << chrsz) - 1;
michael@14 217 for(var i = 0; i < bin.length * 32; i += chrsz)
michael@14 218 str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
michael@14 219 return str;
michael@14 220 }
michael@14 221
michael@14 222 /*
michael@14 223 * Convert an array of little-endian words to a hex string.
michael@14 224 */
michael@14 225 function binl2hex(binarray)
michael@14 226 {
michael@14 227 var hextab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
michael@14 228 var str = "";
michael@14 229 for(var i = 0; i < binarray.length * 4; i++)
michael@14 230 {
michael@14 231 str += hextab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
michael@14 232 hextab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
michael@14 233 }
michael@14 234 return str;
michael@14 235 }
michael@14 236
michael@14 237 /*
michael@14 238 * Convert an array of little-endian words to a base-64 string
michael@14 239 */
michael@14 240 function binl2b64(binarray)
michael@14 241 {
michael@14 242 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
michael@14 243 var str = "";
michael@14 244 for(var i = 0; i < binarray.length * 4; i += 3)
michael@14 245 {
michael@14 246 var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)
michael@14 247 | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
michael@14 248 | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
michael@14 249 for(var j = 0; j < 4; j++)
michael@14 250 {
michael@14 251 if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
michael@14 252 else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
michael@14 253 }
michael@14 254 }
michael@14 255 return str;
michael@14 256 }

mercurial