PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/core/md5sum.js

https://github.com/cramforce/mu
JavaScript | 238 lines | 155 code | 28 blank | 55 comment | 17 complexity | 720cdb39f3fb9b3c7d5fd695d62aa7df MD5 | raw file
  1. /**
  2. * Derived from http://pajhome.org.uk/crypt/md5/md5.html.
  3. * Copyright (C) Paul Johnston 1999 - 2009
  4. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  5. *
  6. * Copyright (c) 2009, Facebook
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * * Neither the name Facebook nor the names of its contributors may be used to
  18. * endorse or promote products derived from this software without specific
  19. * prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. *
  34. *
  35. * @provides fb.md5sum
  36. * @requires fb.prelude
  37. */
  38. /**
  39. * Generate MD5 Sum for the given input string.
  40. *
  41. * @access private
  42. * @param input {String} the data
  43. * @return {String} the hex md5
  44. */
  45. FB.md5sum = function(input) {
  46. // FLOW: input -> utf8 input -> bin input -> bin md5 -> utf8 md5 -> hex md5
  47. var
  48. hex_vocab = '0123456789abcdef',
  49. raw_input = '',
  50. raw_input_bits_len,
  51. bin_input,
  52. bin_md5,
  53. raw_md5 = '',
  54. hex_md5 = '',
  55. i = -1,
  56. x,
  57. y;
  58. function safe_add(x, y) {
  59. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  60. return (((x >> 16) + (y >> 16) + (lsw >> 16)) << 16) | (lsw & 0xFFFF);
  61. }
  62. // basic operations
  63. function cmn(q, a, b, x, s, t) {
  64. var num = safe_add(safe_add(a, q), safe_add(x, t));
  65. return safe_add((num << s) | (num >>> (32 - s)), b);
  66. }
  67. function ff(a, b, c, d, x, s, t) {
  68. return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  69. }
  70. function gg(a, b, c, d, x, s, t) {
  71. return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  72. }
  73. function hh(a, b, c, d, x, s, t) {
  74. return cmn(b ^ c ^ d, a, b, x, s, t);
  75. }
  76. function ii(a, b, c, d, x, s, t) {
  77. return cmn(c ^ (b | (~d)), a, b, x, s, t);
  78. }
  79. // encode string to utf-8
  80. while (++i < input.length) {
  81. /* decode utf-16 surrogate pairs */
  82. x = input.charCodeAt(i);
  83. y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
  84. if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
  85. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  86. i++;
  87. }
  88. /* encode raw_input as utf-8 */
  89. if (x <= 0x7F) {
  90. raw_input += String.fromCharCode(x);
  91. } else if (x <= 0x7FF) {
  92. raw_input += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
  93. 0x80 | ( x & 0x3F));
  94. } else if (x <= 0xFFFF) {
  95. raw_input += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  96. 0x80 | ((x >>> 6 ) & 0x3F),
  97. 0x80 | ( x & 0x3F));
  98. } else if (x <= 0x1FFFFF) {
  99. raw_input += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  100. 0x80 | ((x >>> 12) & 0x3F),
  101. 0x80 | ((x >>> 6 ) & 0x3F),
  102. 0x80 | ( x & 0x3F));
  103. }
  104. }
  105. // number of bits in the raw utf-8 string
  106. raw_input_bits_len = raw_input.length * 8;
  107. // string to little-endian array words
  108. bin_input = Array(raw_input.length >> 2);
  109. for (i = 0; i < bin_input.length; i++) {
  110. bin_input[i] = 0;
  111. }
  112. for (i = 0; i < raw_input_bits_len; i += 8) {
  113. bin_input[i>>5] |= (raw_input.charCodeAt(i / 8) & 0xFF) << (i%32);
  114. }
  115. // calculate md5 as little-endian array words
  116. // padding
  117. bin_input[raw_input_bits_len >> 5] |= 0x80 << ((raw_input_bits_len) % 32);
  118. bin_input[(((raw_input_bits_len + 64) >>> 9) << 4) + 14] = raw_input_bits_len;
  119. var
  120. a = 1732584193,
  121. b = -271733879,
  122. c = -1732584194,
  123. d = 271733878;
  124. for (i = 0; i < bin_input.length; i += 16) {
  125. var
  126. olda = a,
  127. oldb = b,
  128. oldc = c,
  129. oldd = d;
  130. a = ff(a, b, c, d, bin_input[i+ 0], 7 , -680876936);
  131. d = ff(d, a, b, c, bin_input[i+ 1], 12, -389564586);
  132. c = ff(c, d, a, b, bin_input[i+ 2], 17, 606105819);
  133. b = ff(b, c, d, a, bin_input[i+ 3], 22, -1044525330);
  134. a = ff(a, b, c, d, bin_input[i+ 4], 7 , -176418897);
  135. d = ff(d, a, b, c, bin_input[i+ 5], 12, 1200080426);
  136. c = ff(c, d, a, b, bin_input[i+ 6], 17, -1473231341);
  137. b = ff(b, c, d, a, bin_input[i+ 7], 22, -45705983);
  138. a = ff(a, b, c, d, bin_input[i+ 8], 7 , 1770035416);
  139. d = ff(d, a, b, c, bin_input[i+ 9], 12, -1958414417);
  140. c = ff(c, d, a, b, bin_input[i+10], 17, -42063);
  141. b = ff(b, c, d, a, bin_input[i+11], 22, -1990404162);
  142. a = ff(a, b, c, d, bin_input[i+12], 7 , 1804603682);
  143. d = ff(d, a, b, c, bin_input[i+13], 12, -40341101);
  144. c = ff(c, d, a, b, bin_input[i+14], 17, -1502002290);
  145. b = ff(b, c, d, a, bin_input[i+15], 22, 1236535329);
  146. a = gg(a, b, c, d, bin_input[i+ 1], 5 , -165796510);
  147. d = gg(d, a, b, c, bin_input[i+ 6], 9 , -1069501632);
  148. c = gg(c, d, a, b, bin_input[i+11], 14, 643717713);
  149. b = gg(b, c, d, a, bin_input[i+ 0], 20, -373897302);
  150. a = gg(a, b, c, d, bin_input[i+ 5], 5 , -701558691);
  151. d = gg(d, a, b, c, bin_input[i+10], 9 , 38016083);
  152. c = gg(c, d, a, b, bin_input[i+15], 14, -660478335);
  153. b = gg(b, c, d, a, bin_input[i+ 4], 20, -405537848);
  154. a = gg(a, b, c, d, bin_input[i+ 9], 5 , 568446438);
  155. d = gg(d, a, b, c, bin_input[i+14], 9 , -1019803690);
  156. c = gg(c, d, a, b, bin_input[i+ 3], 14, -187363961);
  157. b = gg(b, c, d, a, bin_input[i+ 8], 20, 1163531501);
  158. a = gg(a, b, c, d, bin_input[i+13], 5 , -1444681467);
  159. d = gg(d, a, b, c, bin_input[i+ 2], 9 , -51403784);
  160. c = gg(c, d, a, b, bin_input[i+ 7], 14, 1735328473);
  161. b = gg(b, c, d, a, bin_input[i+12], 20, -1926607734);
  162. a = hh(a, b, c, d, bin_input[i+ 5], 4 , -378558);
  163. d = hh(d, a, b, c, bin_input[i+ 8], 11, -2022574463);
  164. c = hh(c, d, a, b, bin_input[i+11], 16, 1839030562);
  165. b = hh(b, c, d, a, bin_input[i+14], 23, -35309556);
  166. a = hh(a, b, c, d, bin_input[i+ 1], 4 , -1530992060);
  167. d = hh(d, a, b, c, bin_input[i+ 4], 11, 1272893353);
  168. c = hh(c, d, a, b, bin_input[i+ 7], 16, -155497632);
  169. b = hh(b, c, d, a, bin_input[i+10], 23, -1094730640);
  170. a = hh(a, b, c, d, bin_input[i+13], 4 , 681279174);
  171. d = hh(d, a, b, c, bin_input[i+ 0], 11, -358537222);
  172. c = hh(c, d, a, b, bin_input[i+ 3], 16, -722521979);
  173. b = hh(b, c, d, a, bin_input[i+ 6], 23, 76029189);
  174. a = hh(a, b, c, d, bin_input[i+ 9], 4 , -640364487);
  175. d = hh(d, a, b, c, bin_input[i+12], 11, -421815835);
  176. c = hh(c, d, a, b, bin_input[i+15], 16, 530742520);
  177. b = hh(b, c, d, a, bin_input[i+ 2], 23, -995338651);
  178. a = ii(a, b, c, d, bin_input[i+ 0], 6 , -198630844);
  179. d = ii(d, a, b, c, bin_input[i+ 7], 10, 1126891415);
  180. c = ii(c, d, a, b, bin_input[i+14], 15, -1416354905);
  181. b = ii(b, c, d, a, bin_input[i+ 5], 21, -57434055);
  182. a = ii(a, b, c, d, bin_input[i+12], 6 , 1700485571);
  183. d = ii(d, a, b, c, bin_input[i+ 3], 10, -1894986606);
  184. c = ii(c, d, a, b, bin_input[i+10], 15, -1051523);
  185. b = ii(b, c, d, a, bin_input[i+ 1], 21, -2054922799);
  186. a = ii(a, b, c, d, bin_input[i+ 8], 6 , 1873313359);
  187. d = ii(d, a, b, c, bin_input[i+15], 10, -30611744);
  188. c = ii(c, d, a, b, bin_input[i+ 6], 15, -1560198380);
  189. b = ii(b, c, d, a, bin_input[i+13], 21, 1309151649);
  190. a = ii(a, b, c, d, bin_input[i+ 4], 6 , -145523070);
  191. d = ii(d, a, b, c, bin_input[i+11], 10, -1120210379);
  192. c = ii(c, d, a, b, bin_input[i+ 2], 15, 718787259);
  193. b = ii(b, c, d, a, bin_input[i+ 9], 21, -343485551);
  194. a = safe_add(a, olda);
  195. b = safe_add(b, oldb);
  196. c = safe_add(c, oldc);
  197. d = safe_add(d, oldd);
  198. }
  199. bin_md5 = [a, b, c, d];
  200. // little-endian array words to a string
  201. for (i = 0; i < bin_md5.length * 32; i += 8) {
  202. raw_md5 += String.fromCharCode((bin_md5[i>>5] >>> (i % 32)) & 0xFF);
  203. }
  204. // convert the raw md5 string to a hex md5 string
  205. for (i = 0; i < raw_md5.length; i++) {
  206. x = raw_md5.charCodeAt(i);
  207. hex_md5 += hex_vocab.charAt((x >>> 4) & 0x0F) + hex_vocab.charAt(x & 0x0F);
  208. }
  209. return hex_md5;
  210. };