md5.js 5.6 KB

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