Advertisement
Guest User

startup

a guest
Dec 18th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.31 KB | None | 0 0
  1. --[[ MONITOR PIN KEYPAD LOCK
  2. MADE BY JIAN AKA GOOCHGUY
  3. NO WATERMARKS, IF YOU USE
  4. MY PROGRAM, PLEASE LET
  5. THE PEOPLE WHO SEE IT
  6. KNOW WHERE YOU GOT IT.
  7. DON'T TAKE CREDIT FOR MY WORK.]]--
  8.  
  9.  
  10. term.clear()
  11. term.setCursorPos(1,1)
  12.  
  13. local sound = false -- toggle sound (noteblock detected on initilization)
  14. local m -- monitor
  15. local n -- noteblock
  16.  
  17. local data = {
  18. outputSide = "front" ,
  19. invert = false , -- Whether to invert the redstone output
  20. openTime = 5 , -- Time (in seconds) to keep the door open
  21. gridColor = colors.white , -- Color of the grid (Set to same as bgColor for 'no' grid)
  22. gridBgColor = colors.black , -- bg color of the grid. (usually same color as bgColor)
  23. numColor = colors.lightBlue , -- Color of Keys
  24. bgColor = colors.black , -- Background Color
  25. inputColor = colors.red , -- Input screen color
  26. yVal = 1 , --optionally set the vertical position of the keypad
  27. censor = true --censor the pad input
  28. }
  29.  
  30.  
  31.  
  32. local disp = {}
  33. local obj = {}
  34. local curInput = {}
  35.  
  36. --function checklist
  37.  
  38. local mainLoop
  39. local renderKeys
  40. local getInput
  41. local logInput
  42. local compareInput
  43. local grantAccess
  44. local denyAccess
  45. local door
  46.  
  47. -- SHA256 API
  48.  
  49. local sha256
  50. local digestblock
  51. local initH256
  52. local preproc
  53. local s232num
  54. local num2s
  55. local str2hexa
  56. local rrotate
  57. local lshift
  58. local rshift
  59. local rshift1
  60. local bnot
  61. local band
  62. local bxor
  63. local make_bitop
  64. local make_bitop_uncached
  65. local memoize
  66.  
  67.  
  68. --pad labels   
  69. local _p = {
  70. { " 1 " , " 2 " , " 3 " } ,
  71. { " 4 " , " 5 " , " 6 " } ,
  72. { " 7 " , " 8 " , " 9 " } ,
  73. { "COR" , "CLR" , "ENT" }
  74. }
  75.  
  76. local _pZero = {
  77. { " 1 " , " 2 " , " 3 " } ,
  78. { " 4 " , " 5 " , " 6 " } ,
  79. { " 7 " , " 8 " , " 9 " } ,
  80. { "COR" , " 0 " , "ENT" }
  81. }
  82.  
  83. local function toFile( t , file )
  84.     local str = textutils.serialize(t)
  85.     if fs.exists(file) then fs.delete(file) end
  86.     local f = fs.open(file,"w")
  87.     f.write(str)
  88.     f.close()
  89. end
  90.  
  91. local function fromFile( file )
  92.     if ( not fs.exists(file) ) then error("call invalid file") end
  93.     local f = fs.open( file , "r" )
  94.     local table = textutils.unserialize( f.readAll() )
  95.     f.close()
  96.     return table
  97. end
  98.  
  99.  
  100. local function init()
  101.  
  102. --check for monitor
  103.     for _,v in ipairs( rs.getSides() ) do
  104.         if peripheral.getType( v ) == "monitor" then
  105.             monSide = v
  106.         end
  107.     end
  108.     if monSide == nil then
  109.         error("ERR: NO MONITOR FOUND!")
  110.     else
  111.         m = peripheral.wrap( monSide )
  112.         print( "Monitor found on " .. monSide .. " side." )
  113.     end
  114.    
  115. --check for iron noteblock
  116.  
  117.     for _,v in ipairs( rs.getSides() ) do
  118.         if peripheral.getType( v ) == "note" then
  119.             print("Iron noteblock found, sound enabled!")
  120.             sound = true
  121.             n = peripheral.wrap( v )
  122.         end
  123.     end
  124.  
  125. --setup
  126.     if ( not fs.isDir("pin") ) then fs.makeDir("pin") end
  127.     if fs.exists("pin/data") then
  128.         data = fromFile( "pin/data" )
  129.     else
  130.         repeat
  131.             print("Select Code? (1-5 digit number) ")
  132.             local input = read()
  133.             if #input > 5 then print("PIN TOO LONG.")
  134.             elseif type( string.find( input , "0" ) ) == "number" then print("CANT CONTAIN 0's")
  135.             elseif tonumber(input) == nil then print("NOT A NUMBER")
  136.             else data.pin = sha256(input)
  137.             end
  138.         until ( #input <= 5 ) and ( tonumber( input ) ~= nil ) and ( type( string.find( input , "0" ) ) ~= "number" )
  139.     end
  140.     if data.scale == nil then changeScale() end
  141.     mainLoop()
  142.  
  143. end
  144.  
  145.  
  146.  --change the data.scale of the keypad (autoscaling coming soon)
  147. function changeScale()
  148.     local newScale = 0
  149.     repeat
  150.         print("Input New Scale Value ( 0.5 - 5 )")
  151.         local input = read()
  152.         if ( tonumber(input) == nil ) or ( tonumber(input) < 0.5 ) or ( tonumber(input) > 5 ) then
  153.             print("invalid number")
  154.         else
  155.             data.scale = tonumber(input)
  156.             newScale = 1
  157.         end
  158.     until newScale == 1
  159.     toFile( data , "pin/data")
  160. end
  161.  
  162. function changeColors()
  163.     tried = false
  164.     local new = {}
  165.     local input = ""
  166.  
  167.     repeat
  168.         term.clear()
  169.         term.setCursorPos( 1 , 1 )
  170.         print("Possible choices are:" ..
  171.         "\nwhite , orange , magenta , lightBlue , yellow , lime , pink , gray, " ..
  172.         "lightGray , cyan , purple , blue , brown , green , red , black" ..
  173.         "Case sensitive, no spaces."
  174.         )
  175.  
  176.         if tried == true then
  177.             term.setTextColor(colors.red)
  178.             print("\nOne of your colors was invalid, please try again.")
  179.             term.setTextColor(colors.white)
  180.         end
  181.  
  182.  
  183.         print("\nEnter a desired grid color: ")
  184.         input = read()
  185.         new.gridColor = colors[input]
  186.  
  187.         print("Enter a desired grid background color: ")
  188.         input = read()
  189.         new.gridBgColor = colors[input]
  190.  
  191.         print("Enter a desired background color: ")
  192.         input = read()
  193.         new.bgColor = colors[input]
  194.  
  195.         print("Enter a desired button label color: ")
  196.         input = read()
  197.         new.numColor = colors[input]
  198.  
  199.         print("Enter a desired display text color: ")
  200.         local input = read()
  201.         new.inputColor = colors[input]
  202.  
  203.        
  204.         function checkValidColors(t)
  205.             local valid = true
  206.             for k,v in pairs(new) do
  207.                 if not v then valid = false end
  208.             end
  209.             return valid
  210.         end
  211.  
  212.         tried = true
  213.        
  214.     until checkValidColors() == true
  215.  
  216.     for k,v in pairs(new) do
  217.         data[k] = new[k]
  218.     end
  219.  
  220.     new = nil
  221.  
  222.     toFile( data , "pin/data")
  223. end
  224.  
  225. function changePin()
  226.     local newPin = ""
  227.     repeat
  228.         term.clear()
  229.         print("Enter the desired pin")
  230.         print("Select Code? (1-5 digit number) ")
  231.         local input = read()
  232.         if #input > 5 then print("PIN TOO LONG.")
  233.         elseif type( string.find( input , "0" ) ) == "number" then print("CANT CONTAIN 0's")
  234.         elseif tonumber(input) == nil then print("NOT A NUMBER")
  235.         else data.pin = sha256(input)
  236.         end
  237.     until ( #input <= 5 ) and ( tonumber( input ) ~= nil ) and ( type( string.find( input , "0" ) ) ~= "number" )
  238.     toFile( data , "pin/data")
  239. end
  240.  
  241.  
  242. --create a string containing the numbers already inputted and spaces to fill
  243.  
  244.  
  245. function getCurrentInput()
  246.     local curStr = " "
  247.         for k,v in ipairs(curInput) do
  248.             curStr = curStr .. v .. " "
  249.         end
  250.         if #curInput%2 ~= 0 then
  251.             local curStr = curStr .. " "
  252.         end
  253.         curStr = curStr .. string.rep( " " , 11 - #curStr )
  254.     return curStr
  255. end
  256.  
  257. --render the keypad to the monitor
  258.  
  259. function renderKeys()
  260.     m.setBackgroundColor(data.bgColor)
  261.     m.clear()
  262.     local w , h = m.getSize()
  263.     local _x1 =  math.floor(w/2) - 5
  264.         if type(data.yVal) == "number" then
  265.             _y1 = data.yVal
  266.         else
  267.             _y1 = math.floor(h/2) - 5
  268.         end
  269.     m.setCursorPos(_x1 , _y1)
  270.     m.setTextColor( data.gridColor )
  271.     m.setBackgroundColor( data.gridBgColor )
  272.     m.write("+---+---+---+")
  273.     local _y = _y1+1
  274.     m.setCursorPos( _x1 , _y )
  275.     m.write("|")
  276.     m.setBackgroundColor( data.bgColor )
  277.     m.setTextColor( data.inputColor )
  278.     disp.x , disp.y = m.getCursorPos()
  279.     if data.censor == true then
  280.         local censorStr = string.rep( " *" , #curInput )
  281.                 if #censorStr < 11 then
  282.                     repeat
  283.                         censorStr = censorStr .. " "
  284.                     until #censorStr == 11
  285.                 end
  286.         m.write( censorStr )
  287.     else
  288.         m.write( getCurrentInput() )
  289.     end
  290.     m.setBackgroundColor( data.gridBgColor )
  291.     m.setTextColor( data.gridColor )
  292.     m.write("|")
  293.     local _y = _y+1
  294.     m.setCursorPos( _x1 , _y )
  295.     m.write("+---+---+---+")
  296.     local _y = _y+1    
  297.         for k,v in ipairs( _p ) do
  298.             m.setCursorPos( _x1 , _y )
  299.             m.setTextColor( data.gridColor )
  300.             m.setBackgroundColor( data.gridBgColor )
  301.             m.write("|")
  302.                 for k2,v2 in ipairs(_p[k]) do
  303.                     _num = ( k-1 )*3 + k2
  304.                     obj[_num] = {}
  305.                     obj[_num].label = v2
  306.                     obj[_num].x1 , obj[_num].y = m.getCursorPos()
  307.                     obj[_num].x2 = ( obj[_num].x1 ) + 2
  308.                     if v2 == "COR" then
  309.                         m.setTextColor( colors.white )
  310.                         m.setBackgroundColor( colors.orange )                  
  311.                     elseif v2 == "ENT" then
  312.                         m.setTextColor( colors.white )
  313.                         m.setBackgroundColor( colors.green )
  314.                     elseif v2 == "CLR" then
  315.                         m.setTextColor( colors.white )
  316.                         m.setBackgroundColor( colors.red )
  317.                     else
  318.                         m.setTextColor( data.numColor )
  319.                         m.setBackgroundColor( data.bgColor )
  320.                     end
  321.                     m.write( v2 )
  322.                     m.setBackgroundColor( data.gridBgColor )
  323.                     m.setTextColor( data.gridColor )
  324.                     m.write( "|" )
  325.                 end
  326.             _y = _y + 1
  327.             m.setCursorPos( _x1 , _y )
  328.             m.write( "+---+---+---+" )
  329.             _y = _y + 1
  330.         end
  331. end
  332.  
  333. --deny/grant access
  334.  
  335. function denyAccess()
  336.     m.setCursorPos( disp.x , disp.y )
  337.     m.setBackgroundColor( colors.red )
  338.     m.write("  DENIED   ")
  339.     m.setBackgroundColor( data.bgColor )
  340.     m.setTextColor( data.numColor )
  341.     if sound then  
  342.         n.playNote( 4 , 23 )
  343.         sleep(0.15)
  344.         n.playNote( 4 , 20 )
  345.     end
  346.     sleep(2)
  347.     mainLoop()
  348. end
  349.  
  350. function grantAccess()
  351.     m.setCursorPos( disp.x , disp.y )
  352.     m.setBackgroundColor( colors.green )
  353.     m.write("  GRANTED  ")
  354.     m.setBackgroundColor( data.bgColor )
  355.     m.setTextColor( data.numColor )
  356.     if sound then  
  357.         n.playNote( 4 , 20 )
  358.         sleep(0.15)
  359.         n.playNote( 4 , 23 )
  360.     end
  361.     if data.invert == true then redstone.setOutput( data.outputSide , false ) elseif data.invert == false then redstone.setOutput( data.outputSide , true ) end
  362.     sleep( data.openTime )
  363.     if data.invert == true then redstone.setOutput( data.outputSide , true ) elseif data.invert == false then redstone.setOutput( data.outputSide , false ) end
  364.     curInput = {}
  365.     mainLoop()
  366. end
  367.  
  368. --check if the current input is correct
  369.  
  370. function compareInput()
  371.     local inputStr = ""
  372.         for k,v in ipairs( curInput ) do
  373.             inputStr = inputStr .. v
  374.         end
  375.     inputKey = sha256(inputStr)
  376.     --print( "PIN: " .. data.pin )  
  377.     --print( "INPUT: " .. inputStr )
  378.     curInput = {}
  379.     if data.pin == inputKey then
  380.         grantAccess()
  381.     else
  382.         denyAccess()
  383.     end
  384. end
  385. --record monitor touch
  386.  
  387. function logInput( ... )
  388.     for k,v in ipairs( arg ) do
  389.         print( k .. " " .. v )  
  390.     end
  391. end
  392.  
  393. function getInput()
  394.     local e = { os.pullEvent() }
  395.     if e[1] == "monitor_touch" and e[2] == monSide then
  396.         local x , y = e[3] , e[4]
  397.             for k,v in ipairs(obj) do
  398.                 if ( x >= v.x1 ) and ( x <= v.x2 ) and ( y == v.y ) then
  399.                     if v.label == "CLR" then
  400.                         curInput = {}
  401.                         if sound then n.playNote( 3 , 1 ) end
  402.                     elseif v.label == "ENT" then
  403.                         compareInput()
  404.                     elseif v.label == "COR" then
  405.                         table.remove( curInput )
  406.                         if sound then n.playNote( 3 , 5 ) end
  407.                     elseif ( #curInput < 5 ) then
  408.                         table.insert( curInput , tostring(k) )
  409.                         if sound then n.playNote( 3 , 12 ) end
  410.                     end
  411.                 end
  412.             end
  413.     elseif e[1] == "char" then
  414.         if e[2] == "s" then
  415.             changeScale()
  416.         elseif e[2] == "c" then
  417.             changeColors()
  418.         elseif e[2] == "p" then
  419.             changePin()
  420.         end
  421.     end
  422. end
  423.  
  424. function mainLoop()
  425.     while true do
  426.         m.clear()
  427.         m.setTextScale(data.scale)
  428.         renderKeys()
  429.             term.clear()
  430.             term.setCursorPos( 1 , 1 )
  431.             print("S - Scale")
  432.             print("C - Colors")
  433.             print("P - Pin")
  434.         getInput()
  435.     end
  436. end
  437.  
  438.  
  439. --  
  440. --  Adaptation of the Secure Hashing Algorithm (SHA-244/256)
  441. --  Found Here: http://lua-users.org/wiki/SecureHashAlgorithm
  442. --  
  443. --  Using an adapted version of the bit library
  444. --  Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua
  445. --  
  446.  
  447. local MOD = 2^32
  448. local MODM = MOD-1
  449.  
  450. function memoize(f)
  451.     local mt = {}
  452.     local t = setmetatable({}, mt)
  453.     function mt:__index(k)
  454.         local v = f(k)
  455.         t[k] = v
  456.         return v
  457.     end
  458.     return t
  459. end
  460.  
  461. function make_bitop_uncached(t, m)
  462.     local function bitop(a, b)
  463.         local res,p = 0,1
  464.         while a ~= 0 and b ~= 0 do
  465.             local am, bm = a % m, b % m
  466.             res = res + t[am][bm] * p
  467.             a = (a - am) / m
  468.             b = (b - bm) / m
  469.             p = p*m
  470.         end
  471.         res = res + (a + b) * p
  472.         return res
  473.     end
  474.     return bitop
  475. end
  476.  
  477. function make_bitop(t)
  478.     local op1 = make_bitop_uncached(t,2^1)
  479.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  480.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  481. end
  482.  
  483. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  484.  
  485. function bxor(a, b, c, ...)
  486.     local z = nil
  487.     if b then
  488.         a = a % MOD
  489.         b = b % MOD
  490.         z = bxor1(a, b)
  491.         if c then z = bxor(z, c, ...) end
  492.         return z
  493.     elseif a then return a % MOD
  494.     else return 0 end
  495. end
  496.  
  497. function band(a, b, c, ...)
  498.     local z
  499.     if b then
  500.         a = a % MOD
  501.         b = b % MOD
  502.         z = ((a + b) - bxor1(a,b)) / 2
  503.         if c then z = bit32_band(z, c, ...) end
  504.         return z
  505.     elseif a then return a % MOD
  506.     else return MODM end
  507. end
  508.  
  509. function bnot(x) return (-1 - x) % MOD end
  510.  
  511. function rshift1(a, disp)
  512.     if disp < 0 then return lshift(a,-disp) end
  513.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  514. end
  515.  
  516. function rshift(x, disp)
  517.     if disp > 31 or disp < -31 then return 0 end
  518.     return rshift1(x % MOD, disp)
  519. end
  520.  
  521. function lshift(a, disp)
  522.     if disp < 0 then return rshift(a,-disp) end
  523.     return (a * 2 ^ disp) % 2 ^ 32
  524. end
  525.  
  526. function rrotate(x, disp)
  527.     x = x % MOD
  528.     disp = disp % 32
  529.     local low = band(x, 2 ^ disp - 1)
  530.     return rshift(x, disp) + lshift(low, 32 - disp)
  531. end
  532.  
  533. local k = {
  534.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  535.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  536.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  537.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  538.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  539.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  540.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  541.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  542.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  543.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  544.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  545.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  546.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  547.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  548.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  549.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  550. }
  551.  
  552. function str2hexa(s)
  553.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  554. end
  555.  
  556. function num2s(l, n)
  557.     local s = ""
  558.     for i = 1, n do
  559.         local rem = l % 256
  560.         s = string.char(rem) .. s
  561.         l = (l - rem) / 256
  562.     end
  563.     return s
  564. end
  565.  
  566. function s232num(s, i)
  567.     local n = 0
  568.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  569.     return n
  570. end
  571.  
  572. function preproc(msg, len)
  573.     local extra = 64 - ((len + 9) % 64)
  574.     len = num2s(8 * len, 8)
  575.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  576.     assert(#msg % 64 == 0)
  577.     return msg
  578. end
  579.  
  580. function initH256(H)
  581.     H[1] = 0x6a09e667
  582.     H[2] = 0xbb67ae85
  583.     H[3] = 0x3c6ef372
  584.     H[4] = 0xa54ff53a
  585.     H[5] = 0x510e527f
  586.     H[6] = 0x9b05688c
  587.     H[7] = 0x1f83d9ab
  588.     H[8] = 0x5be0cd19
  589.     return H
  590. end
  591.  
  592. function digestblock(msg, i, H)
  593.     local w = {}
  594.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  595.     for j = 17, 64 do
  596.         local v = w[j - 15]
  597.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  598.         v = w[j - 2]
  599.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  600.     end
  601.  
  602.     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]
  603.     for i = 1, 64 do
  604.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  605.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  606.         local t2 = s0 + maj
  607.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  608.         local ch = bxor (band(e, f), band(bnot(e), g))
  609.         local t1 = h + s1 + ch + k[i] + w[i]
  610.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  611.     end
  612.  
  613.     H[1] = band(H[1] + a)
  614.     H[2] = band(H[2] + b)
  615.     H[3] = band(H[3] + c)
  616.     H[4] = band(H[4] + d)
  617.     H[5] = band(H[5] + e)
  618.     H[6] = band(H[6] + f)
  619.     H[7] = band(H[7] + g)
  620.     H[8] = band(H[8] + h)
  621. end
  622.  
  623. function sha256(msg)
  624.     msg = preproc(msg, #msg)
  625.     local H = initH256({})
  626.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  627.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  628.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  629. end
  630.  
  631. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement