md5.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function md5(bytes) {
  4. const words = uint8ToUint32(bytes);
  5. const md5Bytes = wordsToMd5(words, bytes.length * 8);
  6. return uint32ToUint8(md5Bytes);
  7. }
  8. function uint32ToUint8(input) {
  9. const bytes = new Uint8Array(input.length * 4);
  10. for (let i = 0; i < input.length * 4; i++) {
  11. bytes[i] = (input[i >> 2] >>> ((i % 4) * 8)) & 0xff;
  12. }
  13. return bytes;
  14. }
  15. function getOutputLength(inputLength8) {
  16. return (((inputLength8 + 64) >>> 9) << 4) + 14 + 1;
  17. }
  18. function wordsToMd5(x, len) {
  19. const xpad = new Uint32Array(getOutputLength(len)).fill(0);
  20. xpad.set(x);
  21. xpad[len >> 5] |= 0x80 << len % 32;
  22. xpad[xpad.length - 1] = len;
  23. x = xpad;
  24. let a = 1732584193;
  25. let b = -271733879;
  26. let c = -1732584194;
  27. let d = 271733878;
  28. for (let i = 0; i < x.length; i += 16) {
  29. const olda = a;
  30. const oldb = b;
  31. const oldc = c;
  32. const oldd = d;
  33. a = md5ff(a, b, c, d, x[i], 7, -680876936);
  34. d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
  35. c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
  36. b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
  37. a = md5ff(a, b, c, d, x[i + 4], 7, -176418897);
  38. d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
  39. c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
  40. b = md5ff(b, c, d, a, x[i + 7], 22, -45705983);
  41. a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
  42. d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
  43. c = md5ff(c, d, a, b, x[i + 10], 17, -42063);
  44. b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
  45. a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
  46. d = md5ff(d, a, b, c, x[i + 13], 12, -40341101);
  47. c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
  48. b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
  49. a = md5gg(a, b, c, d, x[i + 1], 5, -165796510);
  50. d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
  51. c = md5gg(c, d, a, b, x[i + 11], 14, 643717713);
  52. b = md5gg(b, c, d, a, x[i], 20, -373897302);
  53. a = md5gg(a, b, c, d, x[i + 5], 5, -701558691);
  54. d = md5gg(d, a, b, c, x[i + 10], 9, 38016083);
  55. c = md5gg(c, d, a, b, x[i + 15], 14, -660478335);
  56. b = md5gg(b, c, d, a, x[i + 4], 20, -405537848);
  57. a = md5gg(a, b, c, d, x[i + 9], 5, 568446438);
  58. d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
  59. c = md5gg(c, d, a, b, x[i + 3], 14, -187363961);
  60. b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
  61. a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
  62. d = md5gg(d, a, b, c, x[i + 2], 9, -51403784);
  63. c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
  64. b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
  65. a = md5hh(a, b, c, d, x[i + 5], 4, -378558);
  66. d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
  67. c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
  68. b = md5hh(b, c, d, a, x[i + 14], 23, -35309556);
  69. a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
  70. d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
  71. c = md5hh(c, d, a, b, x[i + 7], 16, -155497632);
  72. b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
  73. a = md5hh(a, b, c, d, x[i + 13], 4, 681279174);
  74. d = md5hh(d, a, b, c, x[i], 11, -358537222);
  75. c = md5hh(c, d, a, b, x[i + 3], 16, -722521979);
  76. b = md5hh(b, c, d, a, x[i + 6], 23, 76029189);
  77. a = md5hh(a, b, c, d, x[i + 9], 4, -640364487);
  78. d = md5hh(d, a, b, c, x[i + 12], 11, -421815835);
  79. c = md5hh(c, d, a, b, x[i + 15], 16, 530742520);
  80. b = md5hh(b, c, d, a, x[i + 2], 23, -995338651);
  81. a = md5ii(a, b, c, d, x[i], 6, -198630844);
  82. d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
  83. c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
  84. b = md5ii(b, c, d, a, x[i + 5], 21, -57434055);
  85. a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
  86. d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
  87. c = md5ii(c, d, a, b, x[i + 10], 15, -1051523);
  88. b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
  89. a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
  90. d = md5ii(d, a, b, c, x[i + 15], 10, -30611744);
  91. c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
  92. b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
  93. a = md5ii(a, b, c, d, x[i + 4], 6, -145523070);
  94. d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
  95. c = md5ii(c, d, a, b, x[i + 2], 15, 718787259);
  96. b = md5ii(b, c, d, a, x[i + 9], 21, -343485551);
  97. a = safeAdd(a, olda);
  98. b = safeAdd(b, oldb);
  99. c = safeAdd(c, oldc);
  100. d = safeAdd(d, oldd);
  101. }
  102. return Uint32Array.of(a, b, c, d);
  103. }
  104. function uint8ToUint32(input) {
  105. if (input.length === 0) {
  106. return new Uint32Array();
  107. }
  108. const output = new Uint32Array(getOutputLength(input.length * 8)).fill(0);
  109. for (let i = 0; i < input.length; i++) {
  110. output[i >> 2] |= (input[i] & 0xff) << ((i % 4) * 8);
  111. }
  112. return output;
  113. }
  114. function safeAdd(x, y) {
  115. const lsw = (x & 0xffff) + (y & 0xffff);
  116. const msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  117. return (msw << 16) | (lsw & 0xffff);
  118. }
  119. function bitRotateLeft(num, cnt) {
  120. return (num << cnt) | (num >>> (32 - cnt));
  121. }
  122. function md5cmn(q, a, b, x, s, t) {
  123. return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b);
  124. }
  125. function md5ff(a, b, c, d, x, s, t) {
  126. return md5cmn((b & c) | (~b & d), a, b, x, s, t);
  127. }
  128. function md5gg(a, b, c, d, x, s, t) {
  129. return md5cmn((b & d) | (c & ~d), a, b, x, s, t);
  130. }
  131. function md5hh(a, b, c, d, x, s, t) {
  132. return md5cmn(b ^ c ^ d, a, b, x, s, t);
  133. }
  134. function md5ii(a, b, c, d, x, s, t) {
  135. return md5cmn(c ^ (b | ~d), a, b, x, s, t);
  136. }
  137. exports.default = md5;