libs/md5.js

  1. /**
  2. * @license
  3. * Joseph Myers does not specify a particular license for his work.
  4. *
  5. * Author: Joseph Myers
  6. * Accessed from: http://www.myersdaily.org/joseph/javascript/md5.js
  7. *
  8. * Modified by: Owen Leong
  9. */
  10. function md5cycle(x, k) {
  11. var a = x[0],
  12. b = x[1],
  13. c = x[2],
  14. d = x[3];
  15. a = ff(a, b, c, d, k[0], 7, -680876936);
  16. d = ff(d, a, b, c, k[1], 12, -389564586);
  17. c = ff(c, d, a, b, k[2], 17, 606105819);
  18. b = ff(b, c, d, a, k[3], 22, -1044525330);
  19. a = ff(a, b, c, d, k[4], 7, -176418897);
  20. d = ff(d, a, b, c, k[5], 12, 1200080426);
  21. c = ff(c, d, a, b, k[6], 17, -1473231341);
  22. b = ff(b, c, d, a, k[7], 22, -45705983);
  23. a = ff(a, b, c, d, k[8], 7, 1770035416);
  24. d = ff(d, a, b, c, k[9], 12, -1958414417);
  25. c = ff(c, d, a, b, k[10], 17, -42063);
  26. b = ff(b, c, d, a, k[11], 22, -1990404162);
  27. a = ff(a, b, c, d, k[12], 7, 1804603682);
  28. d = ff(d, a, b, c, k[13], 12, -40341101);
  29. c = ff(c, d, a, b, k[14], 17, -1502002290);
  30. b = ff(b, c, d, a, k[15], 22, 1236535329);
  31. a = gg(a, b, c, d, k[1], 5, -165796510);
  32. d = gg(d, a, b, c, k[6], 9, -1069501632);
  33. c = gg(c, d, a, b, k[11], 14, 643717713);
  34. b = gg(b, c, d, a, k[0], 20, -373897302);
  35. a = gg(a, b, c, d, k[5], 5, -701558691);
  36. d = gg(d, a, b, c, k[10], 9, 38016083);
  37. c = gg(c, d, a, b, k[15], 14, -660478335);
  38. b = gg(b, c, d, a, k[4], 20, -405537848);
  39. a = gg(a, b, c, d, k[9], 5, 568446438);
  40. d = gg(d, a, b, c, k[14], 9, -1019803690);
  41. c = gg(c, d, a, b, k[3], 14, -187363961);
  42. b = gg(b, c, d, a, k[8], 20, 1163531501);
  43. a = gg(a, b, c, d, k[13], 5, -1444681467);
  44. d = gg(d, a, b, c, k[2], 9, -51403784);
  45. c = gg(c, d, a, b, k[7], 14, 1735328473);
  46. b = gg(b, c, d, a, k[12], 20, -1926607734);
  47. a = hh(a, b, c, d, k[5], 4, -378558);
  48. d = hh(d, a, b, c, k[8], 11, -2022574463);
  49. c = hh(c, d, a, b, k[11], 16, 1839030562);
  50. b = hh(b, c, d, a, k[14], 23, -35309556);
  51. a = hh(a, b, c, d, k[1], 4, -1530992060);
  52. d = hh(d, a, b, c, k[4], 11, 1272893353);
  53. c = hh(c, d, a, b, k[7], 16, -155497632);
  54. b = hh(b, c, d, a, k[10], 23, -1094730640);
  55. a = hh(a, b, c, d, k[13], 4, 681279174);
  56. d = hh(d, a, b, c, k[0], 11, -358537222);
  57. c = hh(c, d, a, b, k[3], 16, -722521979);
  58. b = hh(b, c, d, a, k[6], 23, 76029189);
  59. a = hh(a, b, c, d, k[9], 4, -640364487);
  60. d = hh(d, a, b, c, k[12], 11, -421815835);
  61. c = hh(c, d, a, b, k[15], 16, 530742520);
  62. b = hh(b, c, d, a, k[2], 23, -995338651);
  63. a = ii(a, b, c, d, k[0], 6, -198630844);
  64. d = ii(d, a, b, c, k[7], 10, 1126891415);
  65. c = ii(c, d, a, b, k[14], 15, -1416354905);
  66. b = ii(b, c, d, a, k[5], 21, -57434055);
  67. a = ii(a, b, c, d, k[12], 6, 1700485571);
  68. d = ii(d, a, b, c, k[3], 10, -1894986606);
  69. c = ii(c, d, a, b, k[10], 15, -1051523);
  70. b = ii(b, c, d, a, k[1], 21, -2054922799);
  71. a = ii(a, b, c, d, k[8], 6, 1873313359);
  72. d = ii(d, a, b, c, k[15], 10, -30611744);
  73. c = ii(c, d, a, b, k[6], 15, -1560198380);
  74. b = ii(b, c, d, a, k[13], 21, 1309151649);
  75. a = ii(a, b, c, d, k[4], 6, -145523070);
  76. d = ii(d, a, b, c, k[11], 10, -1120210379);
  77. c = ii(c, d, a, b, k[2], 15, 718787259);
  78. b = ii(b, c, d, a, k[9], 21, -343485551);
  79. x[0] = add32(a, x[0]);
  80. x[1] = add32(b, x[1]);
  81. x[2] = add32(c, x[2]);
  82. x[3] = add32(d, x[3]);
  83. }
  84. function cmn(q, a, b, x, s, t) {
  85. a = add32(add32(a, q), add32(x, t));
  86. return add32((a << s) | (a >>> (32 - s)), b);
  87. }
  88. function ff(a, b, c, d, x, s, t) {
  89. return cmn((b & c) | (~b & d), a, b, x, s, t);
  90. }
  91. function gg(a, b, c, d, x, s, t) {
  92. return cmn((b & d) | (c & ~d), a, b, x, s, t);
  93. }
  94. function hh(a, b, c, d, x, s, t) {
  95. return cmn(b ^ c ^ d, a, b, x, s, t);
  96. }
  97. function ii(a, b, c, d, x, s, t) {
  98. return cmn(c ^ (b | ~d), a, b, x, s, t);
  99. }
  100. function md51(s) {
  101. // txt = '';
  102. var n = s.length,
  103. state = [1732584193, -271733879, -1732584194, 271733878],
  104. i;
  105. for (i = 64; i <= s.length; i += 64) {
  106. md5cycle(state, md5blk(s.substring(i - 64, i)));
  107. }
  108. s = s.substring(i - 64);
  109. var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  110. for (i = 0; i < s.length; i++)
  111. tail[i >> 2] |= s.charCodeAt(i) << (i % 4 << 3);
  112. tail[i >> 2] |= 0x80 << (i % 4 << 3);
  113. if (i > 55) {
  114. md5cycle(state, tail);
  115. for (i = 0; i < 16; i++) tail[i] = 0;
  116. }
  117. tail[14] = n * 8;
  118. md5cycle(state, tail);
  119. return state;
  120. }
  121. /* there needs to be support for Unicode here,
  122. * unless we pretend that we can redefine the MD-5
  123. * algorithm for multi-byte characters (perhaps
  124. * by adding every four 16-bit characters and
  125. * shortening the sum to 32 bits). Otherwise
  126. * I suggest performing MD-5 as if every character
  127. * was two bytes--e.g., 0040 0025 = @%--but then
  128. * how will an ordinary MD-5 sum be matched?
  129. * There is no way to standardize text to something
  130. * like UTF-8 before transformation; speed cost is
  131. * utterly prohibitive. The JavaScript standard
  132. * itself needs to look at this: it should start
  133. * providing access to strings as preformed UTF-8
  134. * 8-bit unsigned value arrays.
  135. */
  136. function md5blk(s) {
  137. /* I figured global was faster. */
  138. var md5blks = [],
  139. i; /* Andy King said do it this way. */
  140. for (i = 0; i < 64; i += 4) {
  141. md5blks[i >> 2] =
  142. s.charCodeAt(i) +
  143. (s.charCodeAt(i + 1) << 8) +
  144. (s.charCodeAt(i + 2) << 16) +
  145. (s.charCodeAt(i + 3) << 24);
  146. }
  147. return md5blks;
  148. }
  149. var hex_chr = "0123456789abcdef".split("");
  150. function rhex(n) {
  151. var s = "",
  152. j = 0;
  153. for (; j < 4; j++)
  154. s += hex_chr[(n >> (j * 8 + 4)) & 0x0f] + hex_chr[(n >> (j * 8)) & 0x0f];
  155. return s;
  156. }
  157. function hex(x) {
  158. for (var i = 0; i < x.length; i++) x[i] = rhex(x[i]);
  159. return x.join("");
  160. }
  161. // Converts a 4-byte number to byte string
  162. function singleToByteString(n) {
  163. return String.fromCharCode(
  164. (n & 0xff) >> 0,
  165. (n & 0xff00) >> 8,
  166. (n & 0xff0000) >> 16,
  167. (n & 0xff000000) >> 24
  168. );
  169. }
  170. // Converts an array of numbers to a byte string
  171. function toByteString(x) {
  172. return x.map(singleToByteString).join("");
  173. }
  174. // Returns the MD5 hash as a byte string
  175. function md5Bin(s) {
  176. return toByteString(md51(s));
  177. }
  178. // Returns MD5 hash as a hex string
  179. function md5(s) {
  180. return hex(md51(s));
  181. }
  182. var md5Check = md5("hello") != "5d41402abc4b2a76b9719d911017c592";
  183. function add32(a, b) {
  184. if (md5Check) {
  185. /* if the md5Check does not match
  186. the expected value, we're dealing
  187. with an old browser and need
  188. this function. */
  189. var lsw = (a & 0xffff) + (b & 0xffff),
  190. msw = (a >> 16) + (b >> 16) + (lsw >> 16);
  191. return (msw << 16) | (lsw & 0xffff);
  192. } else {
  193. /* this function is much faster,
  194. so if possible we use it. Some IEs
  195. are the only ones I know of that
  196. need the idiotic second function,
  197. generated by an if clause. */
  198. return (a + b) & 0xffffffff;
  199. }
  200. }
  201. export { md5, md5Bin };