md5.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. -- require("lockbox").insecure();
  2. local Bit = require("lockbox.util.bit");
  3. local String = require("string");
  4. local Math = require("math");
  5. local Queue = require("lockbox.util.queue");
  6. local SHIFT = {
  7. 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  8. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
  9. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  10. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
  11. local CONSTANTS = {
  12. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  13. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  14. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  15. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  16. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  17. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  18. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  19. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  20. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  21. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  22. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
  23. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  24. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  25. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  26. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  27. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
  28. local AND = Bit.band;
  29. local OR = Bit.bor;
  30. local NOT = Bit.bnot;
  31. local XOR = Bit.bxor;
  32. local LROT = Bit.lrotate;
  33. local LSHIFT = Bit.lshift;
  34. local RSHIFT = Bit.rshift;
  35. --MD5 is little-endian
  36. local bytes2word = function(b0, b1, b2, b3)
  37. local i = b3; i = LSHIFT(i, 8);
  38. i = OR(i, b2); i = LSHIFT(i, 8);
  39. i = OR(i, b1); i = LSHIFT(i, 8);
  40. i = OR(i, b0);
  41. return i;
  42. end
  43. local word2bytes = function(word)
  44. local b0, b1, b2, b3;
  45. b0 = AND(word, 0xFF); word = RSHIFT(word, 8);
  46. b1 = AND(word, 0xFF); word = RSHIFT(word, 8);
  47. b2 = AND(word, 0xFF); word = RSHIFT(word, 8);
  48. b3 = AND(word, 0xFF);
  49. return b0, b1, b2, b3;
  50. end
  51. local dword2bytes = function(i)
  52. local b4, b5, b6, b7 = word2bytes(Math.floor(i / 0x100000000));
  53. local b0, b1, b2, b3 = word2bytes(i);
  54. return b0, b1, b2, b3, b4, b5, b6, b7;
  55. end
  56. local F = function(x, y, z) return OR(AND(x, y), AND(NOT(x), z)); end
  57. local G = function(x, y, z) return OR(AND(x, z), AND(y, NOT(z))); end
  58. local H = function(x, y, z) return XOR(x, XOR(y, z)); end
  59. local I = function(x, y, z) return XOR(y, OR(x, NOT(z))); end
  60. local MD5 = function()
  61. local queue = Queue();
  62. local A = 0x67452301;
  63. local B = 0xefcdab89;
  64. local C = 0x98badcfe;
  65. local D = 0x10325476;
  66. local public = {};
  67. local processBlock = function()
  68. local a = A;
  69. local b = B;
  70. local c = C;
  71. local d = D;
  72. local X = {};
  73. for i = 1, 16 do
  74. X[i] = bytes2word(queue.pop(), queue.pop(), queue.pop(), queue.pop());
  75. end
  76. for i = 0, 63 do
  77. local f, g, temp;
  78. if (0 <= i) and (i <= 15) then
  79. f = F(b, c, d);
  80. g = i;
  81. elseif (16 <= i) and (i <= 31) then
  82. f = G(b, c, d);
  83. g = (5 * i + 1) % 16;
  84. elseif (32 <= i) and (i <= 47) then
  85. f = H(b, c, d);
  86. g = (3 * i + 5) % 16;
  87. elseif (48 <= i) and (i <= 63) then
  88. f = I(b, c, d);
  89. g = (7 * i) % 16;
  90. end
  91. temp = d;
  92. d = c;
  93. c = b;
  94. b = b + LROT((a + f + CONSTANTS[i + 1] + X[g + 1]), SHIFT[i + 1]);
  95. a = temp;
  96. end
  97. A = AND(A + a, 0xFFFFFFFF);
  98. B = AND(B + b, 0xFFFFFFFF);
  99. C = AND(C + c, 0xFFFFFFFF);
  100. D = AND(D + d, 0xFFFFFFFF);
  101. end
  102. public.init = function()
  103. queue.reset();
  104. A = 0x67452301;
  105. B = 0xefcdab89;
  106. C = 0x98badcfe;
  107. D = 0x10325476;
  108. return public;
  109. end
  110. public.update = function(bytes)
  111. for b in bytes do
  112. queue.push(b);
  113. if(queue.size() >= 64) then processBlock(); end
  114. end
  115. return public;
  116. end
  117. public.finish = function()
  118. local bits = queue.getHead() * 8;
  119. queue.push(0x80);
  120. while ((queue.size() + 7) % 64) < 63 do
  121. queue.push(0x00);
  122. end
  123. local b0, b1, b2, b3, b4, b5, b6, b7 = dword2bytes(bits);
  124. queue.push(b0);
  125. queue.push(b1);
  126. queue.push(b2);
  127. queue.push(b3);
  128. queue.push(b4);
  129. queue.push(b5);
  130. queue.push(b6);
  131. queue.push(b7);
  132. while queue.size() > 0 do
  133. processBlock();
  134. end
  135. return public;
  136. end
  137. public.asBytes = function()
  138. local b0, b1, b2, b3 = word2bytes(A);
  139. local b4, b5, b6, b7 = word2bytes(B);
  140. local b8, b9, b10, b11 = word2bytes(C);
  141. local b12, b13, b14, b15 = word2bytes(D);
  142. return {b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15};
  143. end
  144. public.asHex = function()
  145. local b0, b1, b2, b3 = word2bytes(A);
  146. local b4, b5, b6, b7 = word2bytes(B);
  147. local b8, b9, b10, b11 = word2bytes(C);
  148. local b12, b13, b14, b15 = word2bytes(D);
  149. return String.format("%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  150. b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15);
  151. end
  152. public.asString = function()
  153. local b0, b1, b2, b3 = word2bytes(A);
  154. local b4, b5, b6, b7 = word2bytes(B);
  155. local b8, b9, b10, b11 = word2bytes(C);
  156. local b12, b13, b14, b15 = word2bytes(D);
  157. return string.pack(string.rep('B', 16),
  158. b0, b1, b2, b3, b4, b5, b6, b7, b8,
  159. b9, b10, b11, b12, b13, b14, b15
  160. )
  161. end
  162. return public;
  163. end
  164. return MD5;