Posted By

MMDeveloper on 09/19/08


Tagged

javascript md5


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jamesming


Javascript MD5 function, OOP


 / Published in: JavaScript
 

URL: http://www.mechanicmatt.com

  1. var MD5Class = {
  2. makeHash: function(string) {
  3. var x = Array();
  4. var k, AA, BB, CC, DD, a, b, c, d;
  5. var S11 = 7, S12 = 12, S13 = 17, S14 = 22;
  6. var S21 = 5, S22 = 9 , S23 = 14, S24 = 20;
  7. var S31 = 4, S32 = 11, S33 = 16, S34 = 23;
  8. var S41 = 6, S42 = 10, S43 = 15, S44 = 21;
  9.  
  10. string = this.Utf8Encode(string);
  11.  
  12. x = this.ConvertToWordArray(string);
  13.  
  14. a = 0x67452301;
  15. b = 0xEFCDAB89;
  16. c = 0x98BADCFE;
  17. d = 0x10325476;
  18.  
  19. for (k = 0; k < x.length; k += 16) {
  20. AA = a;
  21. BB = b;
  22. CC = c;
  23. DD = d;
  24. a = this.FF(a, b, c, d,x[k + 0], S11, 0xD76AA478);
  25. d = this.FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
  26. c = this.FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
  27. b = this.FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
  28. a = this.FF(a, b, c, d,x[k + 4], S11, 0xF57C0FAF);
  29. d = this.FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
  30. c = this.FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
  31. b = this.FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
  32. a = this.FF(a, b, c, d,x[k + 8], S11, 0x698098D8);
  33. d = this.FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
  34. c = this.FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
  35. b = this.FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
  36. a = this.FF(a, b, c, d,x[k + 12], S11, 0x6B901122);
  37. d = this.FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
  38. c = this.FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
  39. b = this.FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
  40. a = this.GG(a, b, c, d,x[k + 1], S21, 0xF61E2562);
  41. d = this.GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
  42. c = this.GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
  43. b = this.GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
  44. a = this.GG(a, b, c, d,x[k + 5], S21, 0xD62F105D);
  45. d = this.GG(d, a, b, c, x[k + 10], S22, 0x2441453);
  46. c = this.GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
  47. b = this.GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
  48. a = this.GG(a, b, c, d,x[k + 9], S21, 0x21E1CDE6);
  49. d = this.GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
  50. c = this.GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
  51. b = this.GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
  52. a = this.GG(a, b, c, d,x[k + 13], S21, 0xA9E3E905);
  53. d = this.GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
  54. c = this.GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
  55. b = this.GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
  56. a = this.HH(a, b, c, d,x[k + 5], S31, 0xFFFA3942);
  57. d = this.HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
  58. c = this.HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
  59. b = this.HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
  60. a = this.HH(a, b, c, d,x[k + 1], S31, 0xA4BEEA44);
  61. d = this.HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
  62. c = this.HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
  63. b = this.HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
  64. a = this.HH(a, b, c, d,x[k + 13], S31, 0x289B7EC6);
  65. d = this.HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
  66. c = this.HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
  67. b = this.HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
  68. a = this.HH(a, b, c, d,x[k + 9], S31, 0xD9D4D039);
  69. d = this.HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
  70. c = this.HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
  71. b = this.HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
  72. a = this.II(a, b, c, d,x[k + 0], S41, 0xF4292244);
  73. d = this.II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
  74. c = this.II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
  75. b = this.II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
  76. a = this.II(a, b, c, d,x[k + 12], S41, 0x655B59C3);
  77. d = this.II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
  78. c = this.II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
  79. b = this.II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
  80. a = this.II(a, b, c, d,x[k + 8], S41, 0x6FA87E4F);
  81. d = this.II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
  82. c = this.II(c, d, a, b, x[k + 6], S43, 0xA3014314);
  83. b = this.II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
  84. a = this.II(a, b, c, d,x[k + 4], S41, 0xF7537E82);
  85. d = this.II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
  86. c = this.II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
  87. b = this.II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
  88. a = this.AddUnsigned(a,AA);
  89. b = this.AddUnsigned(b,BB);
  90. c = this.AddUnsigned(c,CC);
  91. d = this.AddUnsigned(d,DD);
  92. }
  93.  
  94. var temp = this.WordToHex(a) + this.WordToHex(b) + this.WordToHex(c) + this.WordToHex(d);
  95.  
  96. return temp.toLowerCase();
  97. },
  98.  
  99. RotateLeft: function (lValue, iShiftBits) {
  100. return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
  101. },
  102.  
  103. AddUnsigned: function (lX, lY) {
  104. var lX4, lY4, lX8, lY8, lResult;
  105. lX8 = (lX & 0x80000000);
  106. lY8 = (lY & 0x80000000);
  107. lX4 = (lX & 0x40000000);
  108. lY4 = (lY & 0x40000000);
  109. lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
  110. if (lX4 & lY4) {
  111. return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
  112. } else {}
  113. if (lX4 | lY4) {
  114. if (lResult & 0x40000000) {
  115. return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
  116. }
  117. else {
  118. return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
  119. }
  120. }
  121. else {
  122. return (lResult ^ lX8 ^ lY8);
  123. }
  124. },
  125.  
  126. F: function (x, y, z) {
  127. return (x & y) | ((~x) & z);
  128. },
  129.  
  130. G: function (x, y, z) {
  131. return (x & z) | (y & (~z));
  132. },
  133.  
  134. H: function (x, y, z) {
  135. return (x ^ y ^ z);
  136. },
  137.  
  138. I: function (x, y, z) {
  139. return (y ^ (x | (~z)));
  140. },
  141.  
  142. FF: function (a, b, c, d, x, s, ac) {
  143. a = this.AddUnsigned(a, this.AddUnsigned(this.AddUnsigned(this.F(b, c, d), x), ac));
  144. return this.AddUnsigned(this.RotateLeft(a, s), b);
  145. },
  146.  
  147. GG: function (a, b, c, d, x, s, ac) {
  148. a = this.AddUnsigned(a, this.AddUnsigned(this.AddUnsigned(this.G(b, c, d), x), ac));
  149. return this.AddUnsigned(this.RotateLeft(a, s), b);
  150. },
  151.  
  152. HH: function (a, b, c, d,x, s,ac) {
  153. a = this.AddUnsigned(a, this.AddUnsigned(this.AddUnsigned(this.H(b, c, d), x), ac));
  154. return this.AddUnsigned(this.RotateLeft(a, s), b);
  155. },
  156.  
  157. II: function (a, b, c, d,x, s,ac) {
  158. a = this.AddUnsigned(a, this.AddUnsigned(this.AddUnsigned(this.I(b, c, d), x), ac));
  159. return this.AddUnsigned(this.RotateLeft(a, s), b);
  160. },
  161.  
  162. ConvertToWordArray: function (string) {
  163. var lWordCount;
  164. var lMessageLength = string.length;
  165. var lNumberOfWords_temp1 =lMessageLength + 8;
  166. var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
  167. var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
  168. var lWordArray = Array(lNumberOfWords - 1);
  169. var lBytePosition = 0;
  170. var lByteCount = 0;
  171. while ( lByteCount < lMessageLength ) {
  172. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  173. lBytePosition = (lByteCount % 4) * 8;
  174. lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<<lBytePosition));
  175. lByteCount++;
  176. }
  177. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  178. lBytePosition = (lByteCount % 4) * 8;
  179. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
  180. lWordArray[lNumberOfWords - 2] = lMessageLength<<3;
  181. lWordArray[lNumberOfWords - 1] = lMessageLength>>>29;
  182. return lWordArray;
  183. },
  184.  
  185. WordToHex: function (lValue) {
  186. var WordToHexValue = "", WordToHexValue_temp = "", lByte, lCount;
  187. for (lCount = 0; lCount <= 3; lCount++) {
  188. lByte = (lValue>>>(lCount * 8)) & 255;
  189. WordToHexValue_temp = "0" + lByte.toString(16);
  190. WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);
  191. }
  192. return WordToHexValue;
  193. },
  194.  
  195. Utf8Encode: function (string) {
  196. string = string.replace(/
  197. /g, "\n");
  198. var utftext = "";
  199.  
  200. for (var n = 0; n < string.length; n++) {
  201.  
  202. var c = string.charCodeAt(n);
  203.  
  204. if (c < 128) {
  205. utftext += String.fromCharCode(c);
  206. }
  207. else if((c > 127) && (c < 2048)) {
  208. utftext += String.fromCharCode((c >> 6) | 192);
  209. utftext += String.fromCharCode((c & 63) | 128);
  210. }
  211. else {
  212. utftext += String.fromCharCode((c >> 12) | 224);
  213. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  214. utftext += String.fromCharCode((c & 63) | 128);
  215. }
  216.  
  217. }
  218.  
  219. return utftext;
  220. }
  221. };
  222.  
  223.  
  224. alert(MD5Class.makeHash("yourString"));

Report this snippet  

You need to login to post a comment.