the_king_jaz

TERMINAL

Feb 4th, 2016
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.52 KB | None | 0 0
  1. local MOD = 2^32
  2. local MODM = MOD-1
  3.  
  4. local function memoize(f)
  5.     local mt = {}
  6.     local t = setmetatable({}, mt)
  7.     function mt:__index(k)
  8.         local v = f(k)
  9.         t[k] = v
  10.         return v
  11.     end
  12.     return t
  13. end
  14.  
  15. local function make_bitop_uncached(t, m)
  16.     local function bitop(a, b)
  17.         local res,p = 0,1
  18.         while a ~= 0 and b ~= 0 do
  19.             local am, bm = a % m, b % m
  20.             res = res + t[am][bm] * p
  21.             a = (a - am) / m
  22.             b = (b - bm) / m
  23.             p = p*m
  24.         end
  25.         res = res + (a + b) * p
  26.         return res
  27.     end
  28.     return bitop
  29. end
  30.  
  31. local function make_bitop(t)
  32.     local op1 = make_bitop_uncached(t,2^1)
  33.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  34.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  35. end
  36.  
  37. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  38.  
  39. local function bxor(a, b, c, ...)
  40.     local z = nil
  41.     if b then
  42.         a = a % MOD
  43.         b = b % MOD
  44.         z = bxor1(a, b)
  45.         if c then z = bxor(z, c, ...) end
  46.         return z
  47.     elseif a then return a % MOD
  48.     else return 0 end
  49. end
  50.  
  51. local function band(a, b, c, ...)
  52.     local z
  53.     if b then
  54.         a = a % MOD
  55.         b = b % MOD
  56.         z = ((a + b) - bxor1(a,b)) / 2
  57.         if c then z = bit32_band(z, c, ...) end
  58.         return z
  59.     elseif a then return a % MOD
  60.     else return MODM end
  61. end
  62.  
  63. local function bnot(x) return (-1 - x) % MOD end
  64.  
  65. local function rshift1(a, disp)
  66.     if disp < 0 then return lshift(a,-disp) end
  67.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  68. end
  69.  
  70. local function rshift(x, disp)
  71.     if disp > 31 or disp < -31 then return 0 end
  72.     return rshift1(x % MOD, disp)
  73. end
  74.  
  75. local function lshift(a, disp)
  76.     if disp < 0 then return rshift(a,-disp) end
  77.     return (a * 2 ^ disp) % 2 ^ 32
  78. end
  79.  
  80. local function rrotate(x, disp)
  81.     x = x % MOD
  82.     disp = disp % 32
  83.     local low = band(x, 2 ^ disp - 1)
  84.     return rshift(x, disp) + lshift(low, 32 - disp)
  85. end
  86.  
  87. local k = {
  88.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  89.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  90.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  91.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  92.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  93.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  94.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  95.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  96.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  97.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  98.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  99.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  100.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  101.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  102.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  103.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  104. }
  105.  
  106. local function str2hexa(s)
  107.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  108. end
  109.  
  110. local function num2s(l, n)
  111.     local s = ""
  112.     for i = 1, n do
  113.         local rem = l % 256
  114.         s = string.char(rem) .. s
  115.         l = (l - rem) / 256
  116.     end
  117.     return s
  118. end
  119.  
  120. local function s232num(s, i)
  121.     local n = 0
  122.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  123.     return n
  124. end
  125.  
  126. local function preproc(msg, len)
  127.     local extra = 64 - ((len + 9) % 64)
  128.     len = num2s(8 * len, 8)
  129.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  130.     assert(#msg % 64 == 0)
  131.     return msg
  132. end
  133.  
  134. local function initH256(H)
  135.     H[1] = 0x6a09e667
  136.     H[2] = 0xbb67ae85
  137.     H[3] = 0x3c6ef372
  138.     H[4] = 0xa54ff53a
  139.     H[5] = 0x510e527f
  140.     H[6] = 0x9b05688c
  141.     H[7] = 0x1f83d9ab
  142.     H[8] = 0x5be0cd19
  143.     return H
  144. end
  145.  
  146. local function digestblock(msg, i, H)
  147.     local w = {}
  148.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  149.     for j = 17, 64 do
  150.         local v = w[j - 15]
  151.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  152.         v = w[j - 2]
  153.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  154.     end
  155.  
  156.     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]
  157.     for i = 1, 64 do
  158.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  159.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  160.         local t2 = s0 + maj
  161.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  162.         local ch = bxor (band(e, f), band(bnot(e), g))
  163.         local t1 = h + s1 + ch + k[i] + w[i]
  164.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  165.     end
  166.  
  167.     H[1] = band(H[1] + a)
  168.     H[2] = band(H[2] + b)
  169.     H[3] = band(H[3] + c)
  170.     H[4] = band(H[4] + d)
  171.     H[5] = band(H[5] + e)
  172.     H[6] = band(H[6] + f)
  173.     H[7] = band(H[7] + g)
  174.     H[8] = band(H[8] + h)
  175. end
  176.  
  177. local function sha256(msg)
  178.     msg = preproc(msg, #msg)
  179.     local H = initH256({})
  180.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  181.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  182.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  183. end
  184.  
  185. errorCode = "**ERROR_CODE_0001**"
  186. accounts = {}
  187. os.pullEvent = os.pullEventRaw
  188. side2 = "bottom"
  189. proto = "CCBZ_DATA_RETRIEVAL"
  190. side = "back"
  191. function reset()
  192.   BTC = colors.lightGray
  193.   BGC = colors.black
  194.   TXC = colors.white
  195.   TTC = colors.gray
  196. end
  197. reset()
  198.  
  199. rednet.open(side,true)
  200. term.clear()
  201. term.setCursorPos(1,1)
  202. function mainMenu()
  203.   while true do
  204.     clear()
  205.     y2 = 8
  206.     x2 = 10
  207.     while y2 ~= 13 do
  208.       while x2 ~= 20 do
  209.         paintutils.drawPixel(x2,y2,BTC)
  210.         x2 = x2+1
  211.       end
  212.       x2 = 10
  213.       y2 = y2+1
  214.     end
  215.     y2 = 8
  216.     x2 = 32
  217.     while y2 ~= 13 do
  218.       while x2 ~= 42 do
  219.         paintutils.drawPixel(x2,y2,BTC)
  220.         x2 = x2+1
  221.       end
  222.       x2 = 32
  223.       y2 = y2+1
  224.     end
  225.     term.setCursorPos(13,10)
  226.     print("CCBZ")
  227.     term.setCursorPos(13,11)
  228.     print("Card")
  229.     term.setCursorPos(33,10)
  230.     print("Account#")
  231.     term.setCursorPos(34,11)
  232.     print("& Pass")
  233.     while true do
  234.       local event,button,x1,y1 = os.pullEvent("mouse_click")
  235.       if button==1 and x1 > 9 and x1 < 20 and y1 > 7 and y1 < 13 then
  236.         clear()
  237.         ifCard()
  238.         break
  239.       elseif button==1 and x1 > 31 and x1 < 42 and y1 > 7 and y1 < 13 then
  240.         clear()
  241.         ifPass()
  242.         break
  243.       end
  244.     end
  245.   end
  246. end
  247.  
  248. function ifPass()
  249.   clear()
  250.   print("This feature has been disabled in preference for a safer and more secure login procedure.")
  251.   sleep(3)
  252.   clear()
  253. end
  254.  
  255. function clear()
  256.   term.setBackgroundColor(BGC)
  257.   term.clear()
  258.   paintutils.drawLine(1,1,51,1,TTC)
  259.   term.setCursorPos(1,1)
  260.   term.setTextColor(colors.yellow)
  261.   print("CCBZ: ComputerCraft Bank of Zepherra")
  262.   paintutils.drawPixel(51,19,BGC)
  263.   term.setTextColor(TXC)
  264.   term.setCursorPos(1,2)
  265. end
  266.  
  267. function ifCard()
  268.   while true do
  269.     if disk.isPresent(side2)==false then
  270.       while disk.isPresent(side2)==false do
  271.         clear()
  272.         term.setCursorPos(1,2)
  273.         print("Please Enter your CCBZ Banking Card.")
  274.         sleep(1)
  275.         clear()
  276.         sleep(1)
  277.       end
  278.     elseif disk.isPresent(side2)==true then
  279.       if fs.exists("disk/acc")==true then
  280.         clear()
  281.         print("Please enter the account you wish to access.")
  282.         write("Account: ")
  283.   AccNum = read()
  284.         local salt
  285.         for k1, v1 in pairs(fs.list("disk/acc")) do
  286.             if v1 == AccNum then
  287.                 local file = fs.open("disk/acc/"..v1, "r")
  288.                 salt, name = file.readLine(), file.readLine()
  289.                 file.close()
  290.             end
  291.         end
  292.         if not salt then
  293.           print("Account Name Invalid")
  294.           return
  295.         end
  296.         clear()
  297.         print("Please enter your password.")
  298.         write("Password: ")
  299.         pass = sha256(read("*")..salt)
  300.         getAccData()
  301.         if pass1 == errorCode then
  302.           clear()
  303.           print("Your card is not associated with any CCBZ Accounts.")
  304.           sleep(2)
  305.           clear()
  306.           print("Please take your card.")
  307.           disk.eject(side2)
  308.           sleep(2)
  309.           break
  310.         elseif pass~=pass1 then
  311.           clear()
  312.           write("The password you entered didn't match the password associated with the account belonging to this banking card.")
  313.           sleep(2)
  314.           clear()
  315.           print("Please take your card.")
  316.           disk.eject(side2)
  317.           sleep(2)
  318.           break
  319.         elseif pass==pass1 then
  320.           clear()
  321.           print("Welcome, "..name..".")
  322.           sleep(3)
  323.           bank()
  324.           reset()
  325.           break
  326.         end
  327.       else
  328.         clear()
  329.         print("The card you entered is not a registered CCBZ Banking Card.")
  330.         sleep(2)
  331.         clear()
  332.         print("Please take your card.")
  333.         disk.eject(side2)
  334.         sleep(2)
  335.         break
  336.       end
  337.     end
  338.   end
  339. end
  340.  
  341. function getAccData()
  342.   rednet.broadcast(AccNum,proto)
  343.   senderID, pass1 = rednet.receive(proto)
  344. end
  345.  
  346. function bank()
  347.   clear()
  348.   print("Downloading Personalized GUI data...")
  349.   print("Downloading Button Color...")
  350.   rednet.broadcast("colorRequest",proto)
  351.   sleep(.01)
  352.   senderID, message = rednet.receive(proto)
  353.   if message=="accNum" then
  354.     rednet.broadcast(AccNum,proto)
  355.     senderID, BTC = rednet.receive(proto)
  356.     sleep(.01)
  357.     print("Downloading Background Color...")
  358.     rednet.broadcast("background",proto)
  359.     senderID, BGC = rednet.receive(proto)
  360.     sleep(.01)
  361.     print("Downloading Text Color...")
  362.     rednet.broadcast("text",proto)
  363.     senderID, TXC = rednet.receive(proto)
  364.     sleep(.01)
  365.     print("Downloading Title Color...")
  366.     rednet.broadcast("title",proto)
  367.     sleep(.01)
  368.     senderID, TTC = rednet.receive(proto)
  369.     sleep(.01)
  370.     clear()
  371.   end
  372.   while true do
  373.     clear()
  374.     print("Please select an option.")
  375.     x2 = 2
  376.     y2 = 4
  377.     while y2 ~= 16 do
  378.       paintutils.drawLine(x2,y2,(x2+1),y2,BTC)
  379.       y2 = y2+3
  380.     end
  381.     paintutils.drawPixel(51,19,BGC)
  382.     term.setCursorPos(5,4)
  383.     print("Check Balance")
  384.     term.setCursorPos(5,7)
  385.     print("Make a Payment")
  386.     term.setCursorPos(5,10)
  387.     print("Personal Settings")
  388.     term.setCursorPos(5,13)
  389.     print("Exit")
  390.     local event, button, x1, y1 = os.pullEvent("mouse_click")
  391.     if y1==4 then
  392.       if x1==2 or x1==3 then
  393.         bal()
  394.       end
  395.     elseif y1==7 then
  396.       if x1==2 or x1==3 then
  397.         makePay()
  398.       end
  399.     elseif y1==10 then
  400.       if x1==2 or x1==3 then
  401.         pSet()
  402.       end
  403.     elseif y1==13 then
  404.       if x1==2 or x1==3 then
  405.         ex()
  406.         break
  407.       end
  408.     else
  409.       paintutils.drawPixel(51,19,BTC)
  410.     end
  411.   end
  412. end
  413.  
  414. function bal()
  415.   clear()
  416.   rednet.broadcast("bal",proto)
  417.   senderID, message = rednet.receive(proto)
  418.   sleep(.01)
  419.   rednet.broadcast(AccNum,proto)
  420.   senderID, balance = rednet.receive(proto)
  421.   print("Your current remaining balance is:")
  422.   print("$"..balance)
  423.   print("")
  424.   print("Click anywhere to return.")
  425.   os.pullEvent("mouse_click")
  426. end
  427.  
  428. function outPay()
  429.   bal()
  430. end
  431.  
  432. function inPay()
  433.   bal()
  434. end
  435.  
  436. function makePay()
  437.   clear()
  438.   print("Requesting information...")
  439.   rednet.broadcast("reqAcc",proto)
  440.   count = 0
  441.   while message ~= "noFile" do
  442.     count = count+1
  443.     senderID, message = rednet.receive(proto)
  444.     sleep(.01)
  445.     accounts[count] = message
  446.   end
  447.   clear()
  448.   print("Please enter one of the accounts below to deposit to.")
  449.   print("")
  450.   count = 0
  451.   while account ~= "noFile" do
  452.     count = count+1
  453.     account = accounts[count]
  454.     if account ~= "noFile" then
  455.       print(account)
  456.     end
  457.   end
  458.   print("")
  459.   write("Pay to: ")
  460.   inp = read()
  461.   count = 0
  462.   account = ""
  463.   while account ~= "noFile" do
  464.     count = count+1
  465.     account = accounts[count]
  466.     if inp==account then
  467.       clear()
  468.       print("How much would you like to deposit?")
  469.       write("$")
  470.       xDolla = read()
  471.       xDolla = tonumber(xDolla)
  472.       if xDolla==0 or xDolla < 0 then
  473.         clear()
  474.         print("That is an invalid transaction. You must deposite at least $.01.")
  475.         sleep(3)
  476.         clear()
  477.       else
  478.         clear()
  479.         print("Retrieving information...")
  480.         rednet.broadcast("bal",proto)
  481.         senderID, balance = rednet.receive(proto)
  482.         balance = tonumber(balance)
  483.         sleep(2)
  484.         clear()
  485.         if xDolla > balance then
  486.           print("That is an invalid transaction. You have insufficient funds ("..balance..")")
  487.           sleep(3)
  488.           clear()
  489.         else
  490.           clear()
  491.           print("Preparing to make payment...")
  492.           sleep(.02)
  493.           rednet.broadcast("makePayment",proto)
  494.           print("Receiving startup confirmation...")
  495.           senderID, message = rednet.receive(proto)
  496.           sleep(.02)
  497.           print("Sending account access request...")
  498.           rednet.broadcast(inp,proto)
  499.           print("Receiving continue confirmation...")
  500.           senderID, message = rednet.receive(proto)
  501.           sleep(.02)
  502.           print("Sending deposit ammount...")
  503.           rednet.broadcast(xDolla,proto)
  504.           print("Receiving continue confirmation...")
  505.           senderID, message = rednet.receive(proto)
  506.           sleep(.02)
  507.           print("Requesting Balance...")
  508.           rednet.broadcast("bal",proto)
  509.           print("Waiting for Balance...")
  510.           senderID, balance = rednet.receive(proto)
  511.           clear()
  512.           print("Transaction complete.")
  513.           print("Remaining Balance:")
  514.           print("$"..balance)
  515.           sleep(3)
  516.           clear()
  517.           break
  518.         end
  519.       end
  520.     elseif inp=="cancel" then
  521.       clear()
  522.       break
  523.     elseif inp~=account and account~="noFile" then
  524.       clear()
  525.     else
  526.       clear()
  527.       print("That account was not listed.")
  528.       sleep(3)
  529.       clear()
  530.     end
  531.   end
  532. end
  533.  
  534. function pSet()
  535.   while true do
  536.     pSetClear()
  537.     local event, button, x1, y1 = os.pullEvent("mouse_click")
  538.     if y1==1 then
  539.       TTC = TTC*2
  540.       if TTC == 16 then
  541.         TTC = TTC*2
  542.       elseif TTC==65536 then
  543.         TTC = 1
  544.       end
  545.     elseif y1 > 1 and y1 < 5 or y==5 and x1 < 16 or y==7 and x1 > 4 and x1 < 13 then
  546.       TXC = TXC*2
  547.       if TXC == BGC then
  548.         TXC = TXC*2
  549.       end
  550.       if TXC == 65536 then
  551.         TXC = 1
  552.       end
  553.     elseif y1==7 and x1 > 1 and x1 < 4 then
  554.       BTC = BTC*2
  555.       if BTC == BGC then
  556.         BTC = BTC*2
  557.       end
  558.       if BTC == 65536 then
  559.         BTC = 1
  560.       end
  561.     elseif y1==9 and x1 > 1 and x1 < 4 then
  562.       clear()
  563.       pSetSave()
  564.       break
  565.     else
  566.       BGC = BGC*2
  567.       while BGC==BTC or BGC==TXC do
  568.         if BGC==BTC then
  569.           BGC = BGC*2
  570.         elseif BGC==TXC then
  571.           BGC = BGC*2
  572.         end
  573.         if BGC==65536 then
  574.           BGC = 1
  575.         end
  576.       end
  577.       if BGC==65536 then
  578.         BGC = 1
  579.       end
  580.     end
  581.   end
  582. end
  583.  
  584. function pSetClear()
  585.   clear()
  586.   print("Click anywhere on the screen to cycle through the colors to your prefered settings, then click the exit button. You can click on this text to change the text color.")
  587.   paintutils.drawLine(2,7,3,7,BTC)
  588.   paintutils.drawLine(2,9,3,9,BTC)
  589.   paintutils.drawPixel(51,19,BGC)
  590.   term.setCursorPos(5,7)
  591.   print("<--Button")
  592.   term.setCursorPos(5,9)
  593.   print("Exit")
  594. end
  595.  
  596. function pSetSave()
  597.   clear()
  598.   print("Preparing to save data...")
  599.   rednet.broadcast("personalize",proto)
  600.   senderID, message = rednet.receive(proto)
  601.   sleep(.01)
  602.   print("Saving Button Colors...")
  603.   rednet.broadcast(BTC,proto)
  604.   senderID, message = rednet.receive(proto)
  605.   sleep(.01)
  606.   print("Saving Background Colors...")
  607.   rednet.broadcast(BGC,proto)
  608.   senderID, message = rednet.receive(proto)
  609.   sleep(.01)
  610.   print("Saving Text Colors...")
  611.   rednet.broadcast(TXC,proto)
  612.   senderID, message = rednet.receive(proto)
  613.   sleep(.01)
  614.   print("Saving Titlebar Colors...")
  615.   rednet.broadcast(TTC,proto)
  616.   senderID, message = rednet.receive(proto)
  617.   clear()
  618. end
  619.  
  620. function ex()
  621.   reset()
  622.   clear()
  623.   disk.eject("bottom")
  624. end
  625.  
  626. mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment