sha2_256.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. local Bit = require("bit32"); -- make it with pcall bit
  2. local String = require("string");
  3. local Math = require("math");
  4. local Queue = function()
  5. local queue = {};
  6. local tail = 0;
  7. local head = 0;
  8. local public = {};
  9. public.push = function(obj)
  10. queue[head] = obj;
  11. head = head + 1;
  12. return;
  13. end
  14. public.pop = function()
  15. if tail < head
  16. then
  17. local obj = queue[tail];
  18. queue[tail] = nil;
  19. tail = tail + 1;
  20. return obj;
  21. else
  22. return nil;
  23. end
  24. end
  25. public.size = function()
  26. return head - tail;
  27. end
  28. public.getHead = function()
  29. return head;
  30. end
  31. public.getTail = function()
  32. return tail;
  33. end
  34. public.reset = function()
  35. queue = {};
  36. head = 0;
  37. tail = 0;
  38. end
  39. return public;
  40. end
  41. local CONSTANTS = {
  42. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  43. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  44. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  45. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  46. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  47. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  48. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  49. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
  50. local AND = Bit.band;
  51. local OR = Bit.bor;
  52. local NOT = Bit.bnot;
  53. local XOR = Bit.bxor;
  54. local LROT = Bit.lrotate;
  55. local RROT = Bit.rrotate;
  56. local LSHIFT = Bit.lshift;
  57. local RSHIFT = Bit.rshift;
  58. --SHA2 is big-endian
  59. local bytes2word = function(b0,b1,b2,b3)
  60. local i = b0; i = LSHIFT(i,8);
  61. i = OR(i,b1); i = LSHIFT(i,8);
  62. i = OR(i,b2); i = LSHIFT(i,8);
  63. i = OR(i,b3);
  64. return i;
  65. end
  66. local word2bytes = function(word)
  67. local b0,b1,b2,b3;
  68. b3 = AND(word,0xFF); word = RSHIFT(word,8);
  69. b2 = AND(word,0xFF); word = RSHIFT(word,8);
  70. b1 = AND(word,0xFF); word = RSHIFT(word,8);
  71. b0 = AND(word,0xFF);
  72. return b0,b1,b2,b3;
  73. end
  74. local bytes2dword = function(b0,b1,b2,b3,b4,b5,b6,b7)
  75. local i = bytes2word(b0,b1,b2,b3);
  76. local j = bytes2word(b4,b5,b6,b7);
  77. return (i*0x100000000)+j;
  78. end
  79. local dword2bytes = function(i)
  80. local b4,b5,b6,b7 = word2bytes(i);
  81. local b0,b1,b2,b3 = word2bytes(Math.floor(i/0x100000000));
  82. return b0,b1,b2,b3,b4,b5,b6,b7;
  83. end
  84. local SHA2_256 = function()
  85. local queue = Queue();
  86. local h0 = 0x6a09e667;
  87. local h1 = 0xbb67ae85;
  88. local h2 = 0x3c6ef372;
  89. local h3 = 0xa54ff53a;
  90. local h4 = 0x510e527f;
  91. local h5 = 0x9b05688c;
  92. local h6 = 0x1f83d9ab;
  93. local h7 = 0x5be0cd19;
  94. local public = {};
  95. local processBlock = function()
  96. local a = h0;
  97. local b = h1;
  98. local c = h2;
  99. local d = h3;
  100. local e = h4;
  101. local f = h5;
  102. local g = h6;
  103. local h = h7;
  104. local w = {};
  105. for i=0,15 do
  106. w[i] = bytes2word(queue.pop(),queue.pop(),queue.pop(),queue.pop());
  107. end
  108. for i=16,63 do
  109. local s0 = XOR(RROT(w[i-15],7), XOR(RROT(w[i-15],18), RSHIFT(w[i-15],3)));
  110. local s1 = XOR(RROT(w[i-2],17), XOR(RROT(w[i-2], 19), RSHIFT(w[i-2],10)));
  111. w[i] = AND(w[i-16] + s0 + w[i-7] + s1, 0xFFFFFFFF);
  112. end
  113. for i=0,63 do
  114. local s1 = XOR(RROT(e,6), XOR(RROT(e,11),RROT(e,25)));
  115. local ch = XOR(AND(e,f), AND(NOT(e),g));
  116. local temp1 = h + s1 + ch + CONSTANTS[i+1] + w[i];
  117. local s0 = XOR(RROT(a,2), XOR(RROT(a,13), RROT(a,22)));
  118. local maj = XOR(AND(a,b), XOR(AND(a,c), AND(b,c)));
  119. local temp2 = s0 + maj;
  120. h = g;
  121. g = f;
  122. f = e;
  123. e = d + temp1;
  124. d = c;
  125. c = b;
  126. b = a;
  127. a = temp1 + temp2;
  128. end
  129. h0 = AND(h0 + a, 0xFFFFFFFF);
  130. h1 = AND(h1 + b, 0xFFFFFFFF);
  131. h2 = AND(h2 + c, 0xFFFFFFFF);
  132. h3 = AND(h3 + d, 0xFFFFFFFF);
  133. h4 = AND(h4 + e, 0xFFFFFFFF);
  134. h5 = AND(h5 + f, 0xFFFFFFFF);
  135. h6 = AND(h6 + g, 0xFFFFFFFF);
  136. h7 = AND(h7 + h, 0xFFFFFFFF);
  137. end
  138. public.init = function()
  139. queue.reset();
  140. h0 = 0x6a09e667;
  141. h1 = 0xbb67ae85;
  142. h2 = 0x3c6ef372;
  143. h3 = 0xa54ff53a;
  144. h4 = 0x510e527f;
  145. h5 = 0x9b05688c;
  146. h6 = 0x1f83d9ab;
  147. h7 = 0x5be0cd19;
  148. return public;
  149. end
  150. public.update = function(bytes)
  151. for b in bytes do
  152. queue.push(b);
  153. if queue.size() >= 64 then processBlock(); end
  154. end
  155. return public;
  156. end
  157. public.finish = function()
  158. local bits = queue.getHead() * 8;
  159. queue.push(0x80);
  160. while ((queue.size()+7) % 64) < 63 do
  161. queue.push(0x00);
  162. end
  163. local b0,b1,b2,b3,b4,b5,b6,b7 = dword2bytes(bits);
  164. queue.push(b0);
  165. queue.push(b1);
  166. queue.push(b2);
  167. queue.push(b3);
  168. queue.push(b4);
  169. queue.push(b5);
  170. queue.push(b6);
  171. queue.push(b7);
  172. while queue.size() > 0 do
  173. processBlock();
  174. end
  175. return public;
  176. end
  177. public.asBytes = function()
  178. local b0, b1, b2, b3 = word2bytes(h0);
  179. local b4, b5, b6, b7 = word2bytes(h1);
  180. local b8, b9,b10,b11 = word2bytes(h2);
  181. local b12,b13,b14,b15 = word2bytes(h3);
  182. local b16,b17,b18,b19 = word2bytes(h4);
  183. local b20,b21,b22,b23 = word2bytes(h5);
  184. local b24,b25,b26,b27 = word2bytes(h6);
  185. local b28,b29,b30,b31 = word2bytes(h7);
  186. return { b0, b1, b2, b3, b4, b5, b6, b7, b8, b9,b10,b11,b12,b13,b14,b15
  187. ,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31};
  188. end
  189. public.asHex = function()
  190. local b0, b1, b2, b3 = word2bytes(h0);
  191. local b4, b5, b6, b7 = word2bytes(h1);
  192. local b8, b9,b10,b11 = word2bytes(h2);
  193. local b12,b13,b14,b15 = word2bytes(h3);
  194. local b16,b17,b18,b19 = word2bytes(h4);
  195. local b20,b21,b22,b23 = word2bytes(h5);
  196. local b24,b25,b26,b27 = word2bytes(h6);
  197. local b28,b29,b30,b31 = word2bytes(h7);
  198. local fmt = "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
  199. return String.format(fmt, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9,b10,b11,b12,b13,b14,b15
  200. ,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31);
  201. end
  202. return public;
  203. end
  204. return SHA2_256;