Advertisement
smigger22

EpicLock

Feb 27th, 2015
2,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. details = {}
  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. function findPeripheral(Perihp) --Returns side of first matching peripheral matching passed string
  188. for _,s in ipairs(rs.getSides()) do
  189. if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
  190. return s
  191. end
  192. end
  193. return false
  194. end
  195.  
  196. function runPocket()
  197. while true do
  198. term.setBackgroundColour(colours.white)
  199. term.clear()
  200. for i = 1, 3 do
  201. term.setCursorPos(1, i)
  202. term.setBackgroundColour(colours.blue)
  203. term.setTextColour(colours.lightBlue)
  204. term.clearLine()
  205. end
  206. term.setCursorPos(2, 2)
  207. term.write("EpicLock")
  208. term.setCursorPos(6, 6)
  209. term.setBackgroundColour(colours.white)
  210. term.write("Server ID: ")
  211. id = read()
  212. if type(tonumber(id)) == "number" then
  213. term.setCursorPos(6, 6)
  214. term.setBackgroundColour(colours.white)
  215. term.clearLine()
  216. term.write("Password: ")
  217. pass = read("*")
  218. rednet.send(tonumber(id), sha256(pass))
  219. backID, msg, dis = rednet.receive()
  220. if backID == tonumber(id) then
  221. if msg == "Door_Open" then
  222. term.setCursorPos(6, 10)
  223. term.setTextColour(colours.green)
  224. term.write("Success")
  225. sleep(2)
  226. elseif msg == "Door_Fail" then
  227. term.setCursorPos(6, 10)
  228. term.setTextColour(colours.red)
  229. term.write("Failed")
  230. sleep(2)
  231. end
  232. end
  233. end
  234. end
  235. end
  236.  
  237. function run()
  238. while true do
  239. term.setBackgroundColour(colours.white)
  240. term.clear()
  241. for i = 1, 3 do
  242. term.setCursorPos(1, i)
  243. term.setBackgroundColour(colours.blue)
  244. term.setTextColour(colours.lightBlue)
  245. term.clearLine()
  246. end
  247. term.setCursorPos(2, 2)
  248. term.write("EpicLock")
  249. term.setCursorPos(2, 18)
  250. term.setBackgroundColour(colours.white)
  251. print(os.getComputerID())
  252. term.setCursorPos(6, 6)
  253. term.write("Door Side: ")
  254. door = read()
  255. if door:len() == 0 then door = "left" end
  256. term.setCursorPos(6, 6)
  257. term.setBackgroundColour(colours.white)
  258. term.clearLine()
  259. term.write("Password: ")
  260. password = read("*")
  261. if password:len() > 0 then
  262. details[#details + 1] = {["password"] = sha256(password), ["door"] = door}
  263. open = fs.open(".pass", "w")
  264. open.write(textutils.serialize(details))
  265. open.close()
  266. begin()
  267. end
  268. end
  269. end
  270.  
  271. function runRemote()
  272. while true do
  273. term.setCursorPos(6, 6)
  274. term.setBackgroundColour(colours.white)
  275. term.clearLine()
  276. detailOpen = fs.open(".pass", "r")
  277. detailUnserialize = textutils.unserialize(detailOpen.readAll())
  278. detailOpen.close()
  279.  
  280. for k, v in pairs(detailUnserialize) do
  281. password = v["password"]
  282. door = v["door"]
  283.  
  284. id, msg, dis = rednet.receive()
  285. if msg == password then
  286. rednet.send(id, "Door_Open")
  287. rs.setOutput(door, true)
  288. sleep(2)
  289. rs.setOutput(door, false)
  290. elseif msg ~= password then
  291. rednet.send(id, "Door_Fail")
  292. end
  293. end
  294. end
  295. end
  296.  
  297. if not term.isColour() or not term.isColour then
  298. error("EpicLock needs a gold computer!")
  299. end
  300.  
  301. modemSide = findPeripheral("modem")
  302. rednet.open(modemSide)
  303.  
  304. function begin()
  305. runRemote()
  306. end
  307.  
  308. if pocket then
  309. runPocket()
  310. else
  311. if not fs.exists(".pass") then
  312. run()
  313. elseif fs.exists(".pass") then
  314. begin()
  315. end
  316. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement