browserMd5.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. var hashUtils = require('./browserHashUtils');
  2. var Buffer = require('buffer/').Buffer;
  3. var BLOCK_SIZE = 64;
  4. var DIGEST_LENGTH = 16;
  5. var INIT = [
  6. 0x67452301,
  7. 0xefcdab89,
  8. 0x98badcfe,
  9. 0x10325476,
  10. ];
  11. /**
  12. * @api private
  13. */
  14. function Md5() {
  15. this.state = [
  16. 0x67452301,
  17. 0xefcdab89,
  18. 0x98badcfe,
  19. 0x10325476,
  20. ];
  21. this.buffer = new DataView(new ArrayBuffer(BLOCK_SIZE));
  22. this.bufferLength = 0;
  23. this.bytesHashed = 0;
  24. this.finished = false;
  25. }
  26. /**
  27. * @api private
  28. */
  29. module.exports = exports = Md5;
  30. Md5.BLOCK_SIZE = BLOCK_SIZE;
  31. Md5.prototype.update = function (sourceData) {
  32. if (hashUtils.isEmptyData(sourceData)) {
  33. return this;
  34. } else if (this.finished) {
  35. throw new Error('Attempted to update an already finished hash.');
  36. }
  37. var data = hashUtils.convertToBuffer(sourceData);
  38. var position = 0;
  39. var byteLength = data.byteLength;
  40. this.bytesHashed += byteLength;
  41. while (byteLength > 0) {
  42. this.buffer.setUint8(this.bufferLength++, data[position++]);
  43. byteLength--;
  44. if (this.bufferLength === BLOCK_SIZE) {
  45. this.hashBuffer();
  46. this.bufferLength = 0;
  47. }
  48. }
  49. return this;
  50. };
  51. Md5.prototype.digest = function (encoding) {
  52. if (!this.finished) {
  53. var _a = this, buffer = _a.buffer, undecoratedLength = _a.bufferLength, bytesHashed = _a.bytesHashed;
  54. var bitsHashed = bytesHashed * 8;
  55. buffer.setUint8(this.bufferLength++, 128);
  56. // Ensure the final block has enough room for the hashed length
  57. if (undecoratedLength % BLOCK_SIZE >= BLOCK_SIZE - 8) {
  58. for (var i = this.bufferLength; i < BLOCK_SIZE; i++) {
  59. buffer.setUint8(i, 0);
  60. }
  61. this.hashBuffer();
  62. this.bufferLength = 0;
  63. }
  64. for (var i = this.bufferLength; i < BLOCK_SIZE - 8; i++) {
  65. buffer.setUint8(i, 0);
  66. }
  67. buffer.setUint32(BLOCK_SIZE - 8, bitsHashed >>> 0, true);
  68. buffer.setUint32(BLOCK_SIZE - 4, Math.floor(bitsHashed / 0x100000000), true);
  69. this.hashBuffer();
  70. this.finished = true;
  71. }
  72. var out = new DataView(new ArrayBuffer(DIGEST_LENGTH));
  73. for (var i = 0; i < 4; i++) {
  74. out.setUint32(i * 4, this.state[i], true);
  75. }
  76. var buff = new Buffer(out.buffer, out.byteOffset, out.byteLength);
  77. return encoding ? buff.toString(encoding) : buff;
  78. };
  79. Md5.prototype.hashBuffer = function () {
  80. var _a = this, buffer = _a.buffer, state = _a.state;
  81. var a = state[0], b = state[1], c = state[2], d = state[3];
  82. a = ff(a, b, c, d, buffer.getUint32(0, true), 7, 0xd76aa478);
  83. d = ff(d, a, b, c, buffer.getUint32(4, true), 12, 0xe8c7b756);
  84. c = ff(c, d, a, b, buffer.getUint32(8, true), 17, 0x242070db);
  85. b = ff(b, c, d, a, buffer.getUint32(12, true), 22, 0xc1bdceee);
  86. a = ff(a, b, c, d, buffer.getUint32(16, true), 7, 0xf57c0faf);
  87. d = ff(d, a, b, c, buffer.getUint32(20, true), 12, 0x4787c62a);
  88. c = ff(c, d, a, b, buffer.getUint32(24, true), 17, 0xa8304613);
  89. b = ff(b, c, d, a, buffer.getUint32(28, true), 22, 0xfd469501);
  90. a = ff(a, b, c, d, buffer.getUint32(32, true), 7, 0x698098d8);
  91. d = ff(d, a, b, c, buffer.getUint32(36, true), 12, 0x8b44f7af);
  92. c = ff(c, d, a, b, buffer.getUint32(40, true), 17, 0xffff5bb1);
  93. b = ff(b, c, d, a, buffer.getUint32(44, true), 22, 0x895cd7be);
  94. a = ff(a, b, c, d, buffer.getUint32(48, true), 7, 0x6b901122);
  95. d = ff(d, a, b, c, buffer.getUint32(52, true), 12, 0xfd987193);
  96. c = ff(c, d, a, b, buffer.getUint32(56, true), 17, 0xa679438e);
  97. b = ff(b, c, d, a, buffer.getUint32(60, true), 22, 0x49b40821);
  98. a = gg(a, b, c, d, buffer.getUint32(4, true), 5, 0xf61e2562);
  99. d = gg(d, a, b, c, buffer.getUint32(24, true), 9, 0xc040b340);
  100. c = gg(c, d, a, b, buffer.getUint32(44, true), 14, 0x265e5a51);
  101. b = gg(b, c, d, a, buffer.getUint32(0, true), 20, 0xe9b6c7aa);
  102. a = gg(a, b, c, d, buffer.getUint32(20, true), 5, 0xd62f105d);
  103. d = gg(d, a, b, c, buffer.getUint32(40, true), 9, 0x02441453);
  104. c = gg(c, d, a, b, buffer.getUint32(60, true), 14, 0xd8a1e681);
  105. b = gg(b, c, d, a, buffer.getUint32(16, true), 20, 0xe7d3fbc8);
  106. a = gg(a, b, c, d, buffer.getUint32(36, true), 5, 0x21e1cde6);
  107. d = gg(d, a, b, c, buffer.getUint32(56, true), 9, 0xc33707d6);
  108. c = gg(c, d, a, b, buffer.getUint32(12, true), 14, 0xf4d50d87);
  109. b = gg(b, c, d, a, buffer.getUint32(32, true), 20, 0x455a14ed);
  110. a = gg(a, b, c, d, buffer.getUint32(52, true), 5, 0xa9e3e905);
  111. d = gg(d, a, b, c, buffer.getUint32(8, true), 9, 0xfcefa3f8);
  112. c = gg(c, d, a, b, buffer.getUint32(28, true), 14, 0x676f02d9);
  113. b = gg(b, c, d, a, buffer.getUint32(48, true), 20, 0x8d2a4c8a);
  114. a = hh(a, b, c, d, buffer.getUint32(20, true), 4, 0xfffa3942);
  115. d = hh(d, a, b, c, buffer.getUint32(32, true), 11, 0x8771f681);
  116. c = hh(c, d, a, b, buffer.getUint32(44, true), 16, 0x6d9d6122);
  117. b = hh(b, c, d, a, buffer.getUint32(56, true), 23, 0xfde5380c);
  118. a = hh(a, b, c, d, buffer.getUint32(4, true), 4, 0xa4beea44);
  119. d = hh(d, a, b, c, buffer.getUint32(16, true), 11, 0x4bdecfa9);
  120. c = hh(c, d, a, b, buffer.getUint32(28, true), 16, 0xf6bb4b60);
  121. b = hh(b, c, d, a, buffer.getUint32(40, true), 23, 0xbebfbc70);
  122. a = hh(a, b, c, d, buffer.getUint32(52, true), 4, 0x289b7ec6);
  123. d = hh(d, a, b, c, buffer.getUint32(0, true), 11, 0xeaa127fa);
  124. c = hh(c, d, a, b, buffer.getUint32(12, true), 16, 0xd4ef3085);
  125. b = hh(b, c, d, a, buffer.getUint32(24, true), 23, 0x04881d05);
  126. a = hh(a, b, c, d, buffer.getUint32(36, true), 4, 0xd9d4d039);
  127. d = hh(d, a, b, c, buffer.getUint32(48, true), 11, 0xe6db99e5);
  128. c = hh(c, d, a, b, buffer.getUint32(60, true), 16, 0x1fa27cf8);
  129. b = hh(b, c, d, a, buffer.getUint32(8, true), 23, 0xc4ac5665);
  130. a = ii(a, b, c, d, buffer.getUint32(0, true), 6, 0xf4292244);
  131. d = ii(d, a, b, c, buffer.getUint32(28, true), 10, 0x432aff97);
  132. c = ii(c, d, a, b, buffer.getUint32(56, true), 15, 0xab9423a7);
  133. b = ii(b, c, d, a, buffer.getUint32(20, true), 21, 0xfc93a039);
  134. a = ii(a, b, c, d, buffer.getUint32(48, true), 6, 0x655b59c3);
  135. d = ii(d, a, b, c, buffer.getUint32(12, true), 10, 0x8f0ccc92);
  136. c = ii(c, d, a, b, buffer.getUint32(40, true), 15, 0xffeff47d);
  137. b = ii(b, c, d, a, buffer.getUint32(4, true), 21, 0x85845dd1);
  138. a = ii(a, b, c, d, buffer.getUint32(32, true), 6, 0x6fa87e4f);
  139. d = ii(d, a, b, c, buffer.getUint32(60, true), 10, 0xfe2ce6e0);
  140. c = ii(c, d, a, b, buffer.getUint32(24, true), 15, 0xa3014314);
  141. b = ii(b, c, d, a, buffer.getUint32(52, true), 21, 0x4e0811a1);
  142. a = ii(a, b, c, d, buffer.getUint32(16, true), 6, 0xf7537e82);
  143. d = ii(d, a, b, c, buffer.getUint32(44, true), 10, 0xbd3af235);
  144. c = ii(c, d, a, b, buffer.getUint32(8, true), 15, 0x2ad7d2bb);
  145. b = ii(b, c, d, a, buffer.getUint32(36, true), 21, 0xeb86d391);
  146. state[0] = (a + state[0]) & 0xFFFFFFFF;
  147. state[1] = (b + state[1]) & 0xFFFFFFFF;
  148. state[2] = (c + state[2]) & 0xFFFFFFFF;
  149. state[3] = (d + state[3]) & 0xFFFFFFFF;
  150. };
  151. function cmn(q, a, b, x, s, t) {
  152. a = (((a + q) & 0xFFFFFFFF) + ((x + t) & 0xFFFFFFFF)) & 0xFFFFFFFF;
  153. return (((a << s) | (a >>> (32 - s))) + b) & 0xFFFFFFFF;
  154. }
  155. function ff(a, b, c, d, x, s, t) {
  156. return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  157. }
  158. function gg(a, b, c, d, x, s, t) {
  159. return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  160. }
  161. function hh(a, b, c, d, x, s, t) {
  162. return cmn(b ^ c ^ d, a, b, x, s, t);
  163. }
  164. function ii(a, b, c, d, x, s, t) {
  165. return cmn(c ^ (b | (~d)), a, b, x, s, t);
  166. }