Advertisement
Guest User

md5

a guest
May 18th, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  4.  
  5.  * Digest Algorithm, as defined in RFC 1321.
  6.  
  7.  * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  8.  
  9.  * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  10.  
  11.  * Distributed under the BSD License
  12.  
  13.  * See http://pajhome.org.uk/crypt/md5 for more info.
  14.  
  15.  */
  16.  
  17.  
  18.  
  19. /*
  20.  
  21.  * Configurable variables. You may need to tweak these to be compatible with
  22.  
  23.  * the server-side, but the defaults work in most cases.
  24.  
  25.  */
  26.  
  27. var hexcase = 0;   /* hex output format. 0 - lowercase; 1 - uppercase        */
  28.  
  29. var b64pad  = "";  /* base-64 pad character. "=" for strict RFC compliance   */
  30.  
  31.  
  32.  
  33. /*
  34.  
  35.  * These are the functions you'll usually want to call
  36.  
  37.  * They take string arguments and return either hex or base-64 encoded strings
  38.  
  39.  */
  40.  
  41. function hex_md5(s)    { return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
  42.  
  43. function b64_md5(s)    { return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
  44.  
  45. function any_md5(s, e) { return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
  46.  
  47. function hex_hmac_md5(k, d)
  48.  
  49.   { return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
  50.  
  51. function b64_hmac_md5(k, d)
  52.  
  53.   { return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
  54.  
  55. function any_hmac_md5(k, d, e)
  56.  
  57.   { return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. function hmac_digest(key, pwd)
  64.  
  65. {
  66.  
  67. // var pos = url_with_key.search("KEY");
  68.  
  69. // var key = url_with_key.substring(pos+4);
  70.  
  71.  var hmac = hex_hmac_md5(key, pwd);
  72.  
  73. // confirm("PWD="+pwd+"\n"+"KEY="+key+"\n"+"HMAC="+hmac);
  74.  
  75.  return hmac;
  76.  
  77. }
  78.  
  79.  
  80.  
  81. /*
  82.  
  83.  * Perform a simple self-test to see if the VM is working
  84.  
  85.  */
  86.  
  87. function md5_vm_test()
  88.  
  89. {
  90.  
  91.   return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
  92.  
  93. }
  94.  
  95.  
  96.  
  97. /*
  98.  
  99.  * Calculate the MD5 of a raw string
  100.  
  101.  */
  102.  
  103. function rstr_md5(s)
  104.  
  105. {
  106.  
  107.   return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
  108.  
  109. }
  110.  
  111.  
  112.  
  113. /*
  114.  
  115.  * Calculate the HMAC-MD5, of a key and some data (raw strings)
  116.  
  117.  */
  118.  
  119. function rstr_hmac_md5(key, data)
  120.  
  121. {
  122.  
  123.   var bkey = rstr2binl(key);
  124.  
  125.   if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
  126.  
  127.  
  128.  
  129.   var ipad = Array(16), opad = Array(16);
  130.  
  131.   for(var i = 0; i < 16; i++)
  132.  
  133.   {
  134.  
  135.     ipad[i] = bkey[i] ^ 0x36363636;
  136.  
  137.     opad[i] = bkey[i] ^ 0x5C5C5C5C;
  138.  
  139.   }
  140.  
  141.  
  142.  
  143.   var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  144.  
  145.   return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
  146.  
  147. }
  148.  
  149.  
  150.  
  151. /*
  152.  
  153.  * Convert a raw string to a hex string
  154.  
  155.  */
  156.  
  157. function rstr2hex(input)
  158.  
  159. {
  160.  
  161.   try { hexcase } catch(e) { hexcase=0; }
  162.  
  163.   var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  164.  
  165.   var output = "";
  166.  
  167.   var x;
  168.  
  169.   for(var i = 0; i < input.length; i++)
  170.  
  171.   {
  172.  
  173.     x = input.charCodeAt(i);
  174.  
  175.     output += hex_tab.charAt((x >>> 4) & 0x0F)
  176.  
  177.            +  hex_tab.charAt( x        & 0x0F);
  178.  
  179.   }
  180.  
  181.   return output;
  182.  
  183. }
  184.  
  185.  
  186.  
  187. /*
  188.  
  189.  * Convert a raw string to a base-64 string
  190.  
  191.  */
  192.  
  193. function rstr2b64(input)
  194.  
  195. {
  196.  
  197.   try { b64pad } catch(e) { b64pad=''; }
  198.  
  199.   var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  200.  
  201.   var output = "";
  202.  
  203.   var len = input.length;
  204.  
  205.   for(var i = 0; i < len; i += 3)
  206.  
  207.   {
  208.  
  209.     var triplet = (input.charCodeAt(i) << 16)
  210.  
  211.                 | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
  212.  
  213.                 | (i + 2 < len ? input.charCodeAt(i+2)      : 0);
  214.  
  215.     for(var j = 0; j < 4; j++)
  216.  
  217.     {
  218.  
  219.       if(i * 8 + j * 6 > input.length * 8) output += b64pad;
  220.  
  221.       else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
  222.  
  223.     }
  224.  
  225.   }
  226.  
  227.   return output;
  228.  
  229. }
  230.  
  231.  
  232.  
  233. /*
  234.  
  235.  * Convert a raw string to an arbitrary string encoding
  236.  
  237.  */
  238.  
  239. function rstr2any(input, encoding)
  240.  
  241. {
  242.  
  243.   var divisor = encoding.length;
  244.  
  245.   var i, j, q, x, quotient;
  246.  
  247.  
  248.  
  249.   /* Convert to an array of 16-bit big-endian values, forming the dividend */
  250.  
  251.   var dividend = Array(Math.ceil(input.length / 2));
  252.  
  253.   for(i = 0; i < dividend.length; i++)
  254.  
  255.   {
  256.  
  257.     dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  258.  
  259.   }
  260.  
  261.  
  262.  
  263.   /*
  264.  
  265.    * Repeatedly perform a long division. The binary array forms the dividend,
  266.  
  267.    * the length of the encoding is the divisor. Once computed, the quotient
  268.  
  269.    * forms the dividend for the next step. All remainders are stored for later
  270.  
  271.    * use.
  272.  
  273.    */
  274.  
  275.   var full_length = Math.ceil(input.length * 8 /
  276.  
  277.                                     (Math.log(encoding.length) / Math.log(2)));
  278.  
  279.   var remainders = Array(full_length);
  280.  
  281.   for(j = 0; j < full_length; j++)
  282.  
  283.   {
  284.  
  285.     quotient = Array();
  286.  
  287.     x = 0;
  288.  
  289.     for(i = 0; i < dividend.length; i++)
  290.  
  291.     {
  292.  
  293.       x = (x << 16) + dividend[i];
  294.  
  295.       q = Math.floor(x / divisor);
  296.  
  297.       x -= q * divisor;
  298.  
  299.       if(quotient.length > 0 || q > 0)
  300.  
  301.         quotient[quotient.length] = q;
  302.  
  303.     }
  304.  
  305.     remainders[j] = x;
  306.  
  307.     dividend = quotient;
  308.  
  309.   }
  310.  
  311.  
  312.  
  313.   /* Convert the remainders to the output string */
  314.  
  315.   var output = "";
  316.  
  317.   for(i = remainders.length - 1; i >= 0; i--)
  318.  
  319.     output += encoding.charAt(remainders[i]);
  320.  
  321.  
  322.  
  323.   return output;
  324.  
  325. }
  326.  
  327.  
  328.  
  329. /*
  330.  
  331.  * Encode a string as utf-8.
  332.  
  333.  * For efficiency, this assumes the input is valid utf-16.
  334.  
  335.  */
  336.  
  337. function str2rstr_utf8(input)
  338.  
  339. {
  340.  
  341.   var output = "";
  342.  
  343.   var i = -1;
  344.  
  345.   var x, y;
  346.  
  347.  
  348.  
  349.   while(++i < input.length)
  350.  
  351.   {
  352.  
  353.     /* Decode utf-16 surrogate pairs */
  354.  
  355.     x = input.charCodeAt(i);
  356.  
  357.     y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
  358.  
  359.     if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF)
  360.  
  361.     {
  362.  
  363.       x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  364.  
  365.       i++;
  366.  
  367.     }
  368.  
  369.  
  370.  
  371.     /* Encode output as utf-8 */
  372.  
  373.     if(x <= 0x7F)
  374.  
  375.       output += String.fromCharCode(x);
  376.  
  377.     else if(x <= 0x7FF)
  378.  
  379.       output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
  380.  
  381.                                     0x80 | ( x         & 0x3F));
  382.  
  383.     else if(x <= 0xFFFF)
  384.  
  385.       output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  386.  
  387.                                     0x80 | ((x >>> 6 ) & 0x3F),
  388.  
  389.                                     0x80 | ( x         & 0x3F));
  390.  
  391.     else if(x <= 0x1FFFFF)
  392.  
  393.       output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  394.  
  395.                                     0x80 | ((x >>> 12) & 0x3F),
  396.  
  397.                                     0x80 | ((x >>> 6 ) & 0x3F),
  398.  
  399.                                     0x80 | ( x         & 0x3F));
  400.  
  401.   }
  402.  
  403.   return output;
  404.  
  405. }
  406.  
  407.  
  408.  
  409. /*
  410.  
  411.  * Encode a string as utf-16
  412.  
  413.  */
  414.  
  415. function str2rstr_utf16le(input)
  416.  
  417. {
  418.  
  419.   var output = "";
  420.  
  421.   for(var i = 0; i < input.length; i++)
  422.  
  423.     output += String.fromCharCode( input.charCodeAt(i)        & 0xFF,
  424.  
  425.                                   (input.charCodeAt(i) >>> 8) & 0xFF);
  426.  
  427.   return output;
  428.  
  429. }
  430.  
  431.  
  432.  
  433. function str2rstr_utf16be(input)
  434.  
  435. {
  436.  
  437.   var output = "";
  438.  
  439.   for(var i = 0; i < input.length; i++)
  440.  
  441.     output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
  442.  
  443.                                    input.charCodeAt(i)        & 0xFF);
  444.  
  445.   return output;
  446.  
  447. }
  448.  
  449.  
  450.  
  451. /*
  452.  
  453.  * Convert a raw string to an array of little-endian words
  454.  
  455.  * Characters >255 have their high-byte silently ignored.
  456.  
  457.  */
  458.  
  459. function rstr2binl(input)
  460.  
  461. {
  462.  
  463.   var output = Array(input.length >> 2);
  464.  
  465.   for(var i = 0; i < output.length; i++)
  466.  
  467.     output[i] = 0;
  468.  
  469.   for(var i = 0; i < input.length * 8; i += 8)
  470.  
  471.     output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
  472.  
  473.   return output;
  474.  
  475. }
  476.  
  477.  
  478.  
  479. /*
  480.  
  481.  * Convert an array of little-endian words to a string
  482.  
  483.  */
  484.  
  485. function binl2rstr(input)
  486.  
  487. {
  488.  
  489.   var output = "";
  490.  
  491.   for(var i = 0; i < input.length * 32; i += 8)
  492.  
  493.     output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  494.  
  495.   return output;
  496.  
  497. }
  498.  
  499.  
  500.  
  501. /*
  502.  
  503.  * Calculate the MD5 of an array of little-endian words, and a bit length.
  504.  
  505.  */
  506.  
  507. function binl_md5(x, len)
  508.  
  509. {
  510.  
  511.   /* append padding */
  512.  
  513.   x[len >> 5] |= 0x80 << ((len) % 32);
  514.  
  515.   x[(((len + 64) >>> 9) << 4) + 14] = len;
  516.  
  517.  
  518.  
  519.   var a =  1732584193; // 0x67452301
  520.  
  521.   var b = -271733879;  // 0xefcdab89
  522.  
  523.   var c = -1732584194; // 0x98badcfe
  524.  
  525.   var d =  271733878;  // 0x10325476
  526.  
  527.  
  528.  
  529.   for(var i = 0; i < x.length; i += 16)
  530.  
  531.   {
  532.  
  533.     var olda = a;
  534.  
  535.     var oldb = b;
  536.  
  537.     var oldc = c;
  538.  
  539.     var oldd = d;
  540.  
  541.  
  542.  
  543.     a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  544.  
  545.     d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  546.  
  547.     c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
  548.  
  549.     b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  550.  
  551.     a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  552.  
  553.     d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
  554.  
  555.     c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  556.  
  557.     b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  558.  
  559.     a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
  560.  
  561.     d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  562.  
  563.     c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  564.  
  565.     b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  566.  
  567.     a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
  568.  
  569.     d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  570.  
  571.     c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  572.  
  573.     b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);
  574.  
  575.  
  576.  
  577.     a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  578.  
  579.     d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  580.  
  581.     c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
  582.  
  583.     b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  584.  
  585.     a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  586.  
  587.     d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
  588.  
  589.     c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  590.  
  591.     b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  592.  
  593.     a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
  594.  
  595.     d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  596.  
  597.     c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  598.  
  599.     b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
  600.  
  601.     a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  602.  
  603.     d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  604.  
  605.     c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
  606.  
  607.     b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  608.  
  609.  
  610.  
  611.     a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  612.  
  613.     d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  614.  
  615.     c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
  616.  
  617.     b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  618.  
  619.     a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  620.  
  621.     d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
  622.  
  623.     c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  624.  
  625.     b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  626.  
  627.     a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
  628.  
  629.     d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  630.  
  631.     c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  632.  
  633.     b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
  634.  
  635.     a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  636.  
  637.     d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  638.  
  639.     c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
  640.  
  641.     b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  642.  
  643.  
  644.  
  645.     a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  646.  
  647.     d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
  648.  
  649.     c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  650.  
  651.     b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  652.  
  653.     a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
  654.  
  655.     d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  656.  
  657.     c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  658.  
  659.     b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  660.  
  661.     a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
  662.  
  663.     d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  664.  
  665.     c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  666.  
  667.     b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
  668.  
  669.     a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  670.  
  671.     d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  672.  
  673.     c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
  674.  
  675.     b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  676.  
  677.  
  678.  
  679.     a = safe_add(a, olda);
  680.  
  681.     b = safe_add(b, oldb);
  682.  
  683.     c = safe_add(c, oldc);
  684.  
  685.     d = safe_add(d, oldd);
  686.  
  687.   }
  688.  
  689.   return Array(a, b, c, d);
  690.  
  691. }
  692.  
  693.  
  694.  
  695. /*
  696.  
  697.  * These functions implement the four basic operations the algorithm uses.
  698.  
  699.  */
  700.  
  701. function md5_cmn(q, a, b, x, s, t)
  702.  
  703. {
  704.  
  705.   return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  706.  
  707. }
  708.  
  709. function md5_ff(a, b, c, d, x, s, t)
  710.  
  711. {
  712.  
  713.   return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  714.  
  715. }
  716.  
  717. function md5_gg(a, b, c, d, x, s, t)
  718.  
  719. {
  720.  
  721.   return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  722.  
  723. }
  724.  
  725. function md5_hh(a, b, c, d, x, s, t)
  726.  
  727. {
  728.  
  729.   return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  730.  
  731. }
  732.  
  733. function md5_ii(a, b, c, d, x, s, t)
  734.  
  735. {
  736.  
  737.   return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  738.  
  739. }
  740.  
  741.  
  742.  
  743. /*
  744.  
  745.  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  746.  
  747.  * to work around bugs in some JS interpreters.
  748.  
  749.  */
  750.  
  751. function safe_add(x, y)
  752.  
  753. {
  754.  
  755.   var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  756.  
  757.   var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  758.  
  759.   return (msw << 16) | (lsw & 0xFFFF);
  760.  
  761. }
  762.  
  763.  
  764.  
  765. /*
  766.  
  767.  * Bitwise rotate a 32-bit number to the left.
  768.  
  769.  */
  770.  
  771. function bit_rol(num, cnt)
  772.  
  773. {
  774.  
  775.   return (num << cnt) | (num >>> (32 - cnt));
  776.  
  777. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement