NextYearStudios

Untitled

Mar 26th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. local bit32={
  2. band=function(a,b)
  3. local c,pow=0,1;
  4. while a>0 or b>0 do
  5. local bita,bitb=a%2,b%2;
  6. c,a,b,pow=c+bita*bitb*pow,(a-bita)*0.5,(b-bitb)*0.5,pow*2;
  7. end;
  8. return c;
  9. end;
  10. bnot=function(num)
  11. return 4294967295-num;
  12. end;
  13. bor=function(a,b)
  14. a,b=4294967295-a,4294967295-b;
  15. local c,pow=0,1;
  16. while a>0 or b>0 do
  17. local bita,bitb=a%2,b%2;
  18. c,a,b,pow=c+bita*bitb*pow,(a-bita)*0.5,(b-bitb)*0.5,pow*2;
  19. end;
  20. return 4294967295-c;
  21. end;
  22. bxor=function(a,b)
  23. local c,pow=0,1;
  24. while a>0 or b>0 do
  25. local bita,bitb=a%2,b%2;
  26. c,a,b,pow=c+(bita+bitb)%2*pow,(a-bita)*0.5,(b-bitb)*0.5,pow*2;
  27. end;
  28. return c;
  29. end;
  30. lshift=function(num,offset)
  31. return(num*2^offset)%4294967296;
  32. end;
  33. rshift=function(num,offset)
  34. local result=num*0.5^offset;
  35. return result-result%1;
  36. end;
  37. lrotate=function(num,offset)
  38. local high=(num*2^offset)%4294967296;
  39. local low=num*0.5^(32-offset);
  40. return high+low-low%1;
  41. end;
  42. rrotate=function(num,offset)
  43. local low=num*0.5^offset;
  44. local high=(num*2^(32-offset))%4294967296;
  45. return low-low%1+high;
  46. end;
  47. };
  48. local band,rrotate,bxor,rshift,bnot=bit32.band,bit32.rrotate,bit32.bxor,bit32.rshift,bit32.bnot;
  49. local string,setmetatable,table=string,setmetatable,table;
  50. local k={
  51. 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5;
  52. 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5;
  53. 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3;
  54. 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174;
  55. 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc;
  56. 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da;
  57. 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7;
  58. 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967;
  59. 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13;
  60. 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85;
  61. 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3;
  62. 0xd192e819,0xd6990624,0xf40e3585,0x106aa070;
  63. 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5;
  64. 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3;
  65. 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208;
  66. 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2;
  67. };
  68. local function int32tostring(num)
  69. local b0,b1,b2=num%256,num%65536,num%16777216;
  70. return string.char((num-b2)/16777216,(b2-b1)/65536,(b1-b0)/256,b0);
  71. end;
  72. local function int64tostring(num)
  73. local b0,b1,b2,b3,b4,b5,b6=
  74. num%256,num%65536,num%16777216,num%4294967296,
  75. num%1099511627776,num%281474976710656,num%72057594037927936;
  76. return string.char(
  77. (num-b6)/72057594037927936,(b6-b5)/281474976710656,
  78. (b5-b4)/1099511627776,(b4-b3)/4294967296,
  79. (b3-b2)/16777216,(b2-b1)/65536,(b1-b0)/256,b0);
  80. end;
  81. local function toint32(str,i)
  82. local num=0;
  83. for i=i,i+3 do
  84. num=num*256+string.byte(str,i);
  85. end;
  86. return num;
  87. end;
  88. local function preproc(message,len)
  89. local extra=64-((len+1+8)%64);
  90. len=int64tostring(8*len);
  91. message=message.."\128"..string.rep("\0",extra)..len;
  92. return message;
  93. end;
  94. local function initH256(H)
  95. H[1]=0x6a09e667;
  96. H[2]=0xbb67ae85;
  97. H[3]=0x3c6ef372;
  98. H[4]=0xa54ff53a;
  99. H[5]=0x510e527f;
  100. H[6]=0x9b05688c;
  101. H[7]=0x1f83d9ab;
  102. H[8]=0x5be0cd19;
  103. end;
  104. local function digestblock(message,i,H)
  105. local w={};
  106. for j=1,16 do
  107. w[j]=toint32(message,i+(j-1)*4);
  108. end;
  109. for j=17,64 do
  110. local v=w[j-15]%4294967296;
  111. local s0=bxor(bxor(rrotate(v,7),rrotate(v,18)),rshift(v,3));
  112. v=w[j-2]%4294967296;
  113. local s1=bxor(bxor(rrotate(v,17),rrotate(v,19)),rshift(v,10));
  114. w[j]=w[j-16]+s0+w[j-7]+s1;
  115. end;
  116. local a,b,c,d,e,f,g,h=H[1],H[2],H[3],H[4],H[5],H[6],H[7],H[8];
  117. for i=1,64 do
  118. local s0=bxor(bxor(rrotate(a,2),rrotate(a,13)),rrotate(a,22));
  119. local maj=bxor(bxor(band(a,b),band(a,c)),band(b,c));
  120. local t2=s0+maj;
  121. local s1=bxor(bxor(rrotate(e,6),rrotate(e,11)),rrotate(e,25));
  122. local ch=bxor(band(e,f),band(bnot(e),g));
  123. local t1=h+s1+ch+k[i]+w[i];
  124. h=g;
  125. g=f;
  126. f=e;
  127. e=(d+t1)%4294967296;
  128. d=c;
  129. c=b;
  130. b=a;
  131. a=(t1+t2)%4294967296;
  132. end;
  133. H[1]=(H[1]+a)%4294967296;
  134. H[2]=(H[2]+b)%4294967296;
  135. H[3]=(H[3]+c)%4294967296;
  136. H[4]=(H[4]+d)%4294967296;
  137. H[5]=(H[5]+e)%4294967296;
  138. H[6]=(H[6]+f)%4294967296;
  139. H[7]=(H[7]+g)%4294967296;
  140. H[8]=(H[8]+h)%4294967296;
  141. end;
  142. return{
  143. hash=function(message)
  144. message=preproc(message,#message);
  145. local state={};
  146. initH256(state);
  147. for i=1,#message,64 do
  148. digestblock(message,i,state);
  149. end;
  150. return string.format("%08x%08x%08x%08x%08x%08x%08x%08x",unpack(state));
  151. end;
  152. };
Advertisement
Add Comment
Please, Sign In to add comment