index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict'
  2. var inherits = require('inherits')
  3. var HashBase = require('hash-base')
  4. var Buffer = require('safe-buffer').Buffer
  5. var ARRAY16 = new Array(16)
  6. function MD5 () {
  7. HashBase.call(this, 64)
  8. // state
  9. this._a = 0x67452301
  10. this._b = 0xefcdab89
  11. this._c = 0x98badcfe
  12. this._d = 0x10325476
  13. }
  14. inherits(MD5, HashBase)
  15. MD5.prototype._update = function () {
  16. var M = ARRAY16
  17. for (var i = 0; i < 16; ++i) M[i] = this._block.readInt32LE(i * 4)
  18. var a = this._a
  19. var b = this._b
  20. var c = this._c
  21. var d = this._d
  22. a = fnF(a, b, c, d, M[0], 0xd76aa478, 7)
  23. d = fnF(d, a, b, c, M[1], 0xe8c7b756, 12)
  24. c = fnF(c, d, a, b, M[2], 0x242070db, 17)
  25. b = fnF(b, c, d, a, M[3], 0xc1bdceee, 22)
  26. a = fnF(a, b, c, d, M[4], 0xf57c0faf, 7)
  27. d = fnF(d, a, b, c, M[5], 0x4787c62a, 12)
  28. c = fnF(c, d, a, b, M[6], 0xa8304613, 17)
  29. b = fnF(b, c, d, a, M[7], 0xfd469501, 22)
  30. a = fnF(a, b, c, d, M[8], 0x698098d8, 7)
  31. d = fnF(d, a, b, c, M[9], 0x8b44f7af, 12)
  32. c = fnF(c, d, a, b, M[10], 0xffff5bb1, 17)
  33. b = fnF(b, c, d, a, M[11], 0x895cd7be, 22)
  34. a = fnF(a, b, c, d, M[12], 0x6b901122, 7)
  35. d = fnF(d, a, b, c, M[13], 0xfd987193, 12)
  36. c = fnF(c, d, a, b, M[14], 0xa679438e, 17)
  37. b = fnF(b, c, d, a, M[15], 0x49b40821, 22)
  38. a = fnG(a, b, c, d, M[1], 0xf61e2562, 5)
  39. d = fnG(d, a, b, c, M[6], 0xc040b340, 9)
  40. c = fnG(c, d, a, b, M[11], 0x265e5a51, 14)
  41. b = fnG(b, c, d, a, M[0], 0xe9b6c7aa, 20)
  42. a = fnG(a, b, c, d, M[5], 0xd62f105d, 5)
  43. d = fnG(d, a, b, c, M[10], 0x02441453, 9)
  44. c = fnG(c, d, a, b, M[15], 0xd8a1e681, 14)
  45. b = fnG(b, c, d, a, M[4], 0xe7d3fbc8, 20)
  46. a = fnG(a, b, c, d, M[9], 0x21e1cde6, 5)
  47. d = fnG(d, a, b, c, M[14], 0xc33707d6, 9)
  48. c = fnG(c, d, a, b, M[3], 0xf4d50d87, 14)
  49. b = fnG(b, c, d, a, M[8], 0x455a14ed, 20)
  50. a = fnG(a, b, c, d, M[13], 0xa9e3e905, 5)
  51. d = fnG(d, a, b, c, M[2], 0xfcefa3f8, 9)
  52. c = fnG(c, d, a, b, M[7], 0x676f02d9, 14)
  53. b = fnG(b, c, d, a, M[12], 0x8d2a4c8a, 20)
  54. a = fnH(a, b, c, d, M[5], 0xfffa3942, 4)
  55. d = fnH(d, a, b, c, M[8], 0x8771f681, 11)
  56. c = fnH(c, d, a, b, M[11], 0x6d9d6122, 16)
  57. b = fnH(b, c, d, a, M[14], 0xfde5380c, 23)
  58. a = fnH(a, b, c, d, M[1], 0xa4beea44, 4)
  59. d = fnH(d, a, b, c, M[4], 0x4bdecfa9, 11)
  60. c = fnH(c, d, a, b, M[7], 0xf6bb4b60, 16)
  61. b = fnH(b, c, d, a, M[10], 0xbebfbc70, 23)
  62. a = fnH(a, b, c, d, M[13], 0x289b7ec6, 4)
  63. d = fnH(d, a, b, c, M[0], 0xeaa127fa, 11)
  64. c = fnH(c, d, a, b, M[3], 0xd4ef3085, 16)
  65. b = fnH(b, c, d, a, M[6], 0x04881d05, 23)
  66. a = fnH(a, b, c, d, M[9], 0xd9d4d039, 4)
  67. d = fnH(d, a, b, c, M[12], 0xe6db99e5, 11)
  68. c = fnH(c, d, a, b, M[15], 0x1fa27cf8, 16)
  69. b = fnH(b, c, d, a, M[2], 0xc4ac5665, 23)
  70. a = fnI(a, b, c, d, M[0], 0xf4292244, 6)
  71. d = fnI(d, a, b, c, M[7], 0x432aff97, 10)
  72. c = fnI(c, d, a, b, M[14], 0xab9423a7, 15)
  73. b = fnI(b, c, d, a, M[5], 0xfc93a039, 21)
  74. a = fnI(a, b, c, d, M[12], 0x655b59c3, 6)
  75. d = fnI(d, a, b, c, M[3], 0x8f0ccc92, 10)
  76. c = fnI(c, d, a, b, M[10], 0xffeff47d, 15)
  77. b = fnI(b, c, d, a, M[1], 0x85845dd1, 21)
  78. a = fnI(a, b, c, d, M[8], 0x6fa87e4f, 6)
  79. d = fnI(d, a, b, c, M[15], 0xfe2ce6e0, 10)
  80. c = fnI(c, d, a, b, M[6], 0xa3014314, 15)
  81. b = fnI(b, c, d, a, M[13], 0x4e0811a1, 21)
  82. a = fnI(a, b, c, d, M[4], 0xf7537e82, 6)
  83. d = fnI(d, a, b, c, M[11], 0xbd3af235, 10)
  84. c = fnI(c, d, a, b, M[2], 0x2ad7d2bb, 15)
  85. b = fnI(b, c, d, a, M[9], 0xeb86d391, 21)
  86. this._a = (this._a + a) | 0
  87. this._b = (this._b + b) | 0
  88. this._c = (this._c + c) | 0
  89. this._d = (this._d + d) | 0
  90. }
  91. MD5.prototype._digest = function () {
  92. // create padding and handle blocks
  93. this._block[this._blockOffset++] = 0x80
  94. if (this._blockOffset > 56) {
  95. this._block.fill(0, this._blockOffset, 64)
  96. this._update()
  97. this._blockOffset = 0
  98. }
  99. this._block.fill(0, this._blockOffset, 56)
  100. this._block.writeUInt32LE(this._length[0], 56)
  101. this._block.writeUInt32LE(this._length[1], 60)
  102. this._update()
  103. // produce result
  104. var buffer = Buffer.allocUnsafe(16)
  105. buffer.writeInt32LE(this._a, 0)
  106. buffer.writeInt32LE(this._b, 4)
  107. buffer.writeInt32LE(this._c, 8)
  108. buffer.writeInt32LE(this._d, 12)
  109. return buffer
  110. }
  111. function rotl (x, n) {
  112. return (x << n) | (x >>> (32 - n))
  113. }
  114. function fnF (a, b, c, d, m, k, s) {
  115. return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + b) | 0
  116. }
  117. function fnG (a, b, c, d, m, k, s) {
  118. return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + b) | 0
  119. }
  120. function fnH (a, b, c, d, m, k, s) {
  121. return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + b) | 0
  122. }
  123. function fnI (a, b, c, d, m, k, s) {
  124. return (rotl((a + ((c ^ (b | (~d)))) + m + k) | 0, s) + b) | 0
  125. }
  126. module.exports = MD5