Advertisement
Guest User

Password HEhe

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