Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. -- SHA-256
  2.  
  3. local MOD = 2^32
  4. local MODM = MOD-1
  5.  
  6. local function memoize(f)
  7. local mt = {}
  8. local t = setmetatable({}, mt)
  9. function mt:__index(k)
  10. local v = f(k)
  11. t[k] = v
  12. return v
  13. end
  14. return t
  15. end
  16.  
  17. local function make_bitop_uncached(t, m)
  18. local function bitop(a, b)
  19. local res,p = 0,1
  20. while a ~= 0 and b ~= 0 do
  21. local am, bm = a % m, b % m
  22. res = res + t[am][bm] * p
  23. a = (a - am) / m
  24. b = (b - bm) / m
  25. p = p*m
  26. end
  27. res = res + (a + b) * p
  28. return res
  29. end
  30. return bitop
  31. end
  32.  
  33. local function make_bitop(t)
  34. local op1 = make_bitop_uncached(t,2^1)
  35. local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  36. return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  37. end
  38.  
  39. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  40.  
  41. local function bxor(a, b, c, ...)
  42. local z = nil
  43. if b then
  44. a = a % MOD
  45. b = b % MOD
  46. z = bxor1(a, b)
  47. if c then z = bxor(z, c, ...) end
  48. return z
  49. elseif a then return a % MOD
  50. else return 0 end
  51. end
  52.  
  53. local function band(a, b, c, ...)
  54. local z
  55. if b then
  56. a = a % MOD
  57. b = b % MOD
  58. z = ((a + b) - bxor1(a,b)) / 2
  59. if c then z = bit32_band(z, c, ...) end
  60. return z
  61. elseif a then return a % MOD
  62. else return MODM end
  63. end
  64.  
  65. local function bnot(x) return (-1 - x) % MOD end
  66.  
  67. local function rshift1(a, disp)
  68. if disp < 0 then return lshift(a,-disp) end
  69. return math.floor(a % 2 ^ 32 / 2 ^ disp)
  70. end
  71.  
  72. local function rshift(x, disp)
  73. if disp > 31 or disp < -31 then return 0 end
  74. return rshift1(x % MOD, disp)
  75. end
  76.  
  77. local function lshift(a, disp)
  78. if disp < 0 then return rshift(a,-disp) end
  79. return (a * 2 ^ disp) % 2 ^ 32
  80. end
  81.  
  82. local function rrotate(x, disp)
  83. x = x % MOD
  84. disp = disp % 32
  85. local low = band(x, 2 ^ disp - 1)
  86. return rshift(x, disp) + lshift(low, 32 - disp)
  87. end
  88.  
  89. local k = {
  90. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  91. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  92. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  93. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  94. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  95. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  96. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  97. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  98. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  99. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  100. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  101. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  102. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  103. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  104. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  105. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  106. }
  107.  
  108. local function str2hexa(s)
  109. return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  110. end
  111.  
  112. local function num2s(l, n)
  113. local s = ""
  114. for i = 1, n do
  115. local rem = l % 256
  116. s = string.char(rem) .. s
  117. l = (l - rem) / 256
  118. end
  119. return s
  120. end
  121.  
  122. local function s232num(s, i)
  123. local n = 0
  124. for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  125. return n
  126. end
  127.  
  128. local function preproc(msg, len)
  129. local extra = 64 - ((len + 9) % 64)
  130. len = num2s(8 * len, 8)
  131. msg = msg .. "\128" .. string.rep("\0", extra) .. len
  132. assert(#msg % 64 == 0)
  133. return msg
  134. end
  135.  
  136. local function initH256(H)
  137. H[1] = 0x6a09e667
  138. H[2] = 0xbb67ae85
  139. H[3] = 0x3c6ef372
  140. H[4] = 0xa54ff53a
  141. H[5] = 0x510e527f
  142. H[6] = 0x9b05688c
  143. H[7] = 0x1f83d9ab
  144. H[8] = 0x5be0cd19
  145. return H
  146. end
  147.  
  148. local function digestblock(msg, i, H)
  149. local w = {}
  150. for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  151. for j = 17, 64 do
  152. local v = w[j - 15]
  153. local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  154. v = w[j - 2]
  155. w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  156. end
  157.  
  158. 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]
  159. for i = 1, 64 do
  160. local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  161. local maj = bxor(band(a, b), band(a, c), band(b, c))
  162. local t2 = s0 + maj
  163. local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  164. local ch = bxor (band(e, f), band(bnot(e), g))
  165. local t1 = h + s1 + ch + k[i] + w[i]
  166. h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  167. end
  168.  
  169. H[1] = band(H[1] + a)
  170. H[2] = band(H[2] + b)
  171. H[3] = band(H[3] + c)
  172. H[4] = band(H[4] + d)
  173. H[5] = band(H[5] + e)
  174. H[6] = band(H[6] + f)
  175. H[7] = band(H[7] + g)
  176. H[8] = band(H[8] + h)
  177. end
  178.  
  179. local function sha256(msg)
  180. msg = preproc(msg, #msg)
  181. local H = initH256({})
  182. for i = 1, #msg, 64 do digestblock(msg, i, H) end
  183. return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  184. num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  185. end
  186.  
  187.  
  188.  
  189. -- Methods
  190.  
  191. function generateCurrentCode(crop)
  192. time = math.floor(os.time())
  193. secret = "groeignrofein0poefdigj034tgujhbe948zt§&()$u/&$)(rzHJNREIOBGURDFNHBMDPRIHNUBXSODPRHNUBSPDN"
  194. encrypted = sha256(time .. secret);
  195.  
  196. if crop then
  197. index = math.random(0, 60)
  198. return string.sub(encrypted, index, index+4)
  199. else
  200. return encrypted
  201. end
  202. end
  203.  
  204. function checkCurrentCode(tryCode)
  205. code = generateCurrentCode()
  206.  
  207. if string.len(tryCode) ~= 5 then
  208. return false
  209. end
  210.  
  211. if string.match(code, tryCode) then
  212. return true
  213. else
  214. return false
  215. end
  216. end
  217.  
  218.  
  219. math.randomseed(os.time())
  220.  
  221. local code = generateCurrentCode(true)
  222. print(code)
  223.  
  224. print(checkCurrentCode(code))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement