×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Massimo Zappino
Added: Jan 29, 2011 5:05 PM
Views: 187
Tags: algorithm md5
  1. /*
  2.  * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  3.  * Digest Algorithm, as defined in RFC 1321.
  4.  * Copyright (C) Paul Johnston 1999 - 2000.
  5.  * Updated by Greg Holt 2000 - 2001.
  6.  * See http://pajhome.org.uk/site/legal.html for details.
  7.  */
  8.  
  9. /*
  10.  * Convert a 32-bit number to a hex string with ls-byte first
  11.  */
  12. var hex_chr = "0123456789abcdef";
  13. function rhex(num)
  14. {
  15.   str = "";
  16.   for(j = 0; j <= 3; j++)
  17.     str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
  18.            hex_chr.charAt((num >> (j * 8)) & 0x0F);
  19.   return str;
  20. }
  21.  
  22. /*
  23.  * Convert a string to a sequence of 16-word blocks, stored as an array.
  24.  * Append padding bits and the length, as described in the MD5 standard.
  25.  */
  26. function str2blks_MD5(str)
  27. {
  28.   nblk = ((str.length + 8) >> 6) + 1;
  29.   blks = new Array(nblk * 16);
  30.   for(i = 0; i < nblk * 16; i++) blks[i] = 0;
  31.   for(i = 0; i < str.length; i++)
  32.     blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
  33.   blks[i >> 2] |= 0x80 << ((i % 4) * 8);
  34.   blks[nblk * 16 - 2] = str.length * 8;
  35.   return blks;
  36. }
  37.  
  38. /*
  39.  * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  40.  * to work around bugs in some JS interpreters.
  41.  */
  42. function add(x, y)
  43. {
  44.   var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  45.   var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  46.   return (msw << 16) | (lsw & 0xFFFF);
  47. }
  48.  
  49. /*
  50.  * Bitwise rotate a 32-bit number to the left
  51.  */
  52. function rol(num, cnt)
  53. {
  54.   return (num << cnt) | (num >>> (32 - cnt));
  55. }
  56.  
  57. /*
  58.  * These functions implement the basic operation for each round of the
  59.  * algorithm.
  60.  */
  61. function cmn(q, a, b, x, s, t)
  62. {
  63.   return add(rol(add(add(a, q), add(x, t)), s), b);
  64. }
  65. function ff(a, b, c, d, x, s, t)
  66. {
  67.   return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  68. }
  69. function gg(a, b, c, d, x, s, t)
  70. {
  71.   return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  72. }
  73. function hh(a, b, c, d, x, s, t)
  74. {
  75.   return cmn(b ^ c ^ d, a, b, x, s, t);
  76. }
  77. function ii(a, b, c, d, x, s, t)
  78. {
  79.   return cmn(c ^ (b | (~d)), a, b, x, s, t);
  80. }
  81.  
  82. /*
  83.  * Take a string and return the hex representation of its MD5.
  84.  */
  85. function calcMD5(str)
  86. {
  87.   x = str2blks_MD5(str);
  88.   a =  1732584193;
  89.   b = -271733879;
  90.   c = -1732584194;
  91.   d =  271733878;
  92.  
  93.   for(i = 0; i < x.length; i += 16)
  94.   {
  95.     olda = a;
  96.     oldb = b;
  97.     oldc = c;
  98.     oldd = d;
  99.  
  100.     a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  101.     d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
  102.     c = ff(c, d, a, b, x[i+ 2], 17,  606105819);
  103.     b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  104.     a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  105.     d = ff(d, a, b, c, x[i+ 5], 12,  1200080426);
  106.     c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  107.     b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
  108.     a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
  109.     d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  110.     c = ff(c, d, a, b, x[i+10], 17, -42063);
  111.     b = ff(b, c, d, a, x[i+11], 22, -1990404162);
  112.     a = ff(a, b, c, d, x[i+12], 7 ,  1804603682);
  113.     d = ff(d, a, b, c, x[i+13], 12, -40341101);
  114.     c = ff(c, d, a, b, x[i+14], 17, -1502002290);
  115.     b = ff(b, c, d, a, x[i+15], 22,  1236535329);    
  116.  
  117.     a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  118.     d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  119.     c = gg(c, d, a, b, x[i+11], 14,  643717713);
  120.     b = gg(b, c, d, a, x[i+ 0], 20, -373897302);
  121.     a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  122.     d = gg(d, a, b, c, x[i+10], 9 ,  38016083);
  123.     c = gg(c, d, a, b, x[i+15], 14, -660478335);
  124.     b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
  125.     a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
  126.     d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
  127.     c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
  128.     b = gg(b, c, d, a, x[i+ 8], 20,  1163531501);
  129.     a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
  130.     d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  131.     c = gg(c, d, a, b, x[i+ 7], 14,  1735328473);
  132.     b = gg(b, c, d, a, x[i+12], 20, -1926607734);
  133.    
  134.     a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
  135.     d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  136.     c = hh(c, d, a, b, x[i+11], 16,  1839030562);
  137.     b = hh(b, c, d, a, x[i+14], 23, -35309556);
  138.     a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  139.     d = hh(d, a, b, c, x[i+ 4], 11,  1272893353);
  140.     c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
  141.     b = hh(b, c, d, a, x[i+10], 23, -1094730640);
  142.     a = hh(a, b, c, d, x[i+13], 4 ,  681279174);
  143.     d = hh(d, a, b, c, x[i+ 0], 11, -358537222);
  144.     c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
  145.     b = hh(b, c, d, a, x[i+ 6], 23,  76029189);
  146.     a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  147.     d = hh(d, a, b, c, x[i+12], 11, -421815835);
  148.     c = hh(c, d, a, b, x[i+15], 16,  530742520);
  149.     b = hh(b, c, d, a, x[i+ 2], 23, -995338651);
  150.  
  151.     a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  152.     d = ii(d, a, b, c, x[i+ 7], 10,  1126891415);
  153.     c = ii(c, d, a, b, x[i+14], 15, -1416354905);
  154.     b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
  155.     a = ii(a, b, c, d, x[i+12], 6 ,  1700485571);
  156.     d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  157.     c = ii(c, d, a, b, x[i+10], 15, -1051523);
  158.     b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  159.     a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
  160.     d = ii(d, a, b, c, x[i+15], 10, -30611744);
  161.     c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  162.     b = ii(b, c, d, a, x[i+13], 21,  1309151649);
  163.     a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  164.     d = ii(d, a, b, c, x[i+11], 10, -1120210379);
  165.     c = ii(c, d, a, b, x[i+ 2], 15,  718787259);
  166.     b = ii(b, c, d, a, x[i+ 9], 21, -343485551);
  167.  
  168.     a = add(a, olda);
  169.     b = add(b, oldb);
  170.     c = add(c, oldc);
  171.     d = add(d, oldd);
  172.   }
  173.   return rhex(a) + rhex(b) + rhex(c) + rhex(d);
  174. }
  175.  
  176.