Advertisement
LDDestroier

Hashed Computer/Door lock

Dec 8th, 2015
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.23 KB | None | 0 0
  1. --Encrypted password lock door. Uses valithor's tiny hashing algorithm.
  2. -- pastebin get uW0X4aKn lock
  3. --os.pullEvent = os.pullEventRaw --THIS IS DONE FOR A REASON. DO NOT UNCOMMENT.
  4. local doorSide = {} --If it is to side(s), then it will open a door.
  5. local doorCorrectDelay = 6 --How long the door is open after getting it right
  6. local doorIncorrectDelay = 2 --How long you are locked out for getting it wrong
  7. local doorInvertRedstone = false --Inverts redstone for door locks that require it.
  8. local passwordFile = "/.lddlock"
  9. local saltCode = tostring(os.getComputerID()^2)
  10. local palate
  11. if not term.isColor() then
  12.     if _VERSION then
  13.         palate = {
  14.             bgcolor = colors.gray, --Background color
  15.             fgcolor = colors.lightGray, --Foreground color
  16.             bg_txtcolor = colors.white, --Text color when overlayed on bgcolor
  17.             fg_txtcolor = colors.black, --Text color when overlayed on fgcolor
  18.             bgcorrect = colors.white, --Background color when correct.
  19.             bgincorrect = colors.black, --Background color when incorrect.
  20.             txtcorrect = colors.gray, --Text color when correct.
  21.             txtincorrect = colors.gray, --Text color when incorrect.
  22.         }
  23.     else
  24.         palate = {
  25.             bgcolor = colors.black, --Background color
  26.             fgcolor = colors.white, --Foreground color
  27.             bg_txtcolor = colors.white, --Text color when overlayed on bgcolor
  28.             fg_txtcolor = colors.black, --Text color when overlayed on fgcolor
  29.             bgcorrect = colors.white, --Background color when correct.
  30.             bgincorrect = colors.black, --Background color when incorrect.
  31.             txtcorrect = colors.black, --Text color when correct.
  32.             txtincorrect = colors.white, --Text color when incorrect.
  33.         }
  34.     end
  35. else
  36.     palate = {
  37.         bgcolor = colors.gray, --Background color
  38.         fgcolor = colors.lightGray, --Foreground color
  39.         bg_txtcolor = colors.white, --Text color when overlayed on bgcolor
  40.         fg_txtcolor = colors.black, --Text color when overlayed on fgcolor
  41.         bgcorrect = colors.green, --Background color when correct.
  42.         bgincorrect = colors.red, --Background color when incorrect.
  43.         txtcorrect = colors.white, --Text color when correct.
  44.         txtincorrect = colors.black, --Text color when incorrect.
  45.     }
  46. end
  47. for a = 1, #doorSide do
  48.     redstone.setOutput(doorSide[a], doorInvertRedstone)
  49. end
  50.  
  51. local function clearLines(top, bottom)
  52.     for a = top, bottom do
  53.         term.setCursorPos(1,a)
  54.         term.clearLine()
  55.     end
  56. end
  57.  
  58. local function hash(msg,salt,bool)
  59.     if not bool then
  60.         for i = 1, 10 do
  61.             msg = hash(msg,salt or "",true)
  62.         end
  63.     end
  64.     local num = ""
  65.     local salt = salt ~= nil and salt..msg or msg
  66.     for i = 1, #salt do
  67.         local let = salt:sub(i,i):byte()
  68.         num = let <= 9  and num.."99"..let or let<=99 and num.."9"..let or num..let
  69.     end
  70.     math.randomseed(tonumber(num))
  71.     local hashed = ""
  72.     for i = 1, 250 do
  73.         hashed = hashed..string.char(math.random(32,127))
  74.     end
  75.     return hashed
  76. end
  77.  
  78. local function rollOver(input, max)
  79.     return math.floor(input % max)
  80. end
  81.  
  82. local number
  83.  
  84. local function flashScreen(times, number)
  85.     local flashes
  86.     if term.isColor() then
  87.         flashes = {
  88.             colors.white,
  89.             colors.black,
  90.             colors.red,
  91.         }
  92.     else
  93.         if _CC_VERSION then
  94.             flashes = {
  95.                 colors.white,
  96.                 colors.black,
  97.                 colors.lightGray,
  98.             }
  99.         else
  100.             flashes = {
  101.                 colors.white,
  102.                 colors.black,
  103.             }
  104.         end
  105.     end
  106.     local terminateMessages = {
  107.         "TERMINATE AGAIN, I DARE YOU",
  108.         "TERMINATE AGAIN, I DARE YOU",
  109.         "NO PLEASE, I IMPLORE",
  110.         "NO, REALLY",
  111.         "MOST PEOPLE DISABLE TERMINATION...",
  112.         "BUT THAT'S BORING.",
  113.         "THIS IS AT LEAST FUNNY!",
  114.         "I MEAN, YOU DIDN'T EXPECT MULTIPLE MESSAGES",
  115.         "AT LEAST, I DON'T THINK...",
  116.         "...",
  117.         "OKAY, THIS IS GETTING OLD FAST",
  118.         "STOP IT!",
  119.         "STOP TERMINATING, OR ERRORING, OR WHATEVER",
  120.         "I'LL TELL THE ADMIN!",
  121.         "I WILL!",
  122.         "I'M WARNING YOU!",
  123.         ".....",
  124.         "...YOU CALLED MY BLUFF.",
  125.         "OKAY, I CAN DO SOMETHING THAT YOU WON'T LIKE",
  126.         "NO, IT'S NOT TO TATTLE TO A MODERATOR...",
  127.         "IT'S SOMETHING MUCH WORSE.",
  128.         "OKAY NOT REALLY, UNLESS YOU HAVE EPILEPSY",
  129.         "AREN'T YOUR FINGERS GETTING TIRED?",
  130.         "I GUESS NOT...?",
  131.         "OKAY, HERE IT GOES!",
  132.         "YOU'D BETTER HEED MY WARNING!",
  133.         "<<flash>>",
  134.         "WHAT DID I TELL YOU?",
  135.         "YOU THOUGHT YOU'D CALL MY BLUFF, BUT NOOOOO",
  136.         "YOU MUST THINK YOU'RE SOOOOO CLEVER",
  137.         "BUT LOOKIT YOU NOW! ALL DEAD AND STUFF",
  138.         "....",
  139.         "...OKAY, I'M GETTING BORED.",
  140.         "ACTUALLY, I WAS ALREADY BORED...",
  141.         "BUT I THOUGHT THIS WOULD BE FUN",
  142.         "HOW WRONG WAS I?",
  143.         "ANYHOW, JUST...UM...",
  144.         "...DO WHATEVER IT IS YOU DO",
  145.         "WHEN YOU'RE NOT DESTROYING THE FACILITY",
  146.         "YOU SHOULD PLAY PORTAL NOW.",
  147.         "IT'S MUCH MORE FUN THAN TERMINATING A LOCK.",
  148.         "BELIEVE ME, YOU'LL HAVE A GREAT TIME DOING THAT",
  149.         "...",
  150.         "......",
  151.         "OKAY, IF YOU'RE DOING THIS TO FIND OUT THE CODE...",
  152.         "...OR GAIN ACCESS WITHOUT IT...",
  153.         "...THEN YOU ARE WASTING YOUR TIME.",
  154.         "THIS USES HASHING. THE PASSWORD ISN'T STORED...",
  155.         "...IN PLAIN TEXT, THAT IS",
  156.         "IF YOU WANT THAT, THEN GO BEG THE OWNER FOR IT",
  157.         "AND BY THAT I MEAN THE OWNER OF THE COMPUTER",
  158.         "NOT THE GUY WHO WROTE THE LOCK",
  159.         "I'M SURE HE HAS BETTER THINGS TO DO THAN THIS.",
  160.         "THIS IS THE LAST LINE OF DIALOGUE.",
  161.         "THIS IS THE LAST LINE OF DIALOGUE.",
  162.         "THIS IS THE LAST LINE OF DIALOGUE.",
  163.         "THIS IS THE LAST LINE OF DIALOGUE.",
  164.         "THIS IS TOTALLY THE LAST LINE OF DIALOGUE.",
  165.         "GOD DAMN IT, MAN!",
  166.         "YOU WANT ACCESS SO BAD?",
  167.         "YOU DO?",
  168.         "<<flash>>",
  169.         "NOW YOU HAVE ACCESS TO THE FLOOR",
  170.         "DON'T BREAK IT, FATTY",
  171.         "OH, I'M SORRY...",
  172.         "I ONLY SAID THAT BECAUSE...",
  173.         "YOU LOOK LIKE YOU'RE SMUGGLING A CAR",
  174.         "STUPID. STUPID FATTY.",
  175.         "...",
  176.         "*ROBOTIC COUGH*",
  177.         "Damn is this script long!",
  178.         "I can't wait until someone mods this and adds their own!",
  179.         "Welp, I'm not adding anything else.",
  180.         "Really, the last message will repeat.",
  181.         "So, my ultimate message is:",
  182.         "DO NOT TERMINATE.",
  183.     } --THAT was a mouthful!
  184.     if not number then
  185.         number = 1
  186.     end
  187.     if number >= #terminateMessages then
  188.         number = #terminateMessages
  189.     end
  190.     local message = terminateMessages[number]
  191.     local scr_x, scr_y = term.getSize()
  192.     if message == "<<flash>>" then
  193.         for a = 1, 64 do
  194.             sleep(0)
  195.             if term.isColor() then
  196.                 term.setBackgroundColor(2^math.random(0,15))
  197.             else
  198.                 if a % 2 == 0 then
  199.                     term.setBackgroundColor(colors.white)
  200.                 else
  201.                     term.setBackgroundColor(colors.black)
  202.                 end
  203.             end
  204.             term.clear()
  205.         end
  206.     else
  207.         for a = 1, times do
  208.             for b = 1, #flashes do
  209.                 term.setBackgroundColor(flashes[b])
  210.                 term.setTextColor(flashes[rollOver(b+1,#flashes)+1])
  211.                 term.clear()
  212.                 scr_x, scr_y = term.getSize()
  213.                 if #message >= scr_x then
  214.                     term.setCursorPos(1,scr_y/2)
  215.                 else
  216.                     term.setCursorPos(math.ceil(scr_x/2)-math.floor(#message/2),scr_y/2)
  217.                 end
  218.                 write(message)
  219.                 sleep(0)
  220.             end
  221.         end
  222.     end
  223. end
  224.  
  225. local terminateCount = 1
  226.  
  227. local isCurrentlyGood = false
  228.  
  229. local function handleCrash(results)
  230.     if not isCurrentlyGood then
  231.         term.clear()
  232.         term.setCursorBlink(false)
  233.         local oldpullevent = os.pullEvent
  234.         os.pullEvent = os.pullEventRaw
  235.         flashScreen(10, terminateCount) --Gotta rub it in, man.
  236.         os.pullEvent = oldpullevent
  237.         terminateCount = terminateCount + 1
  238.         term.setCursorBlink(true)
  239.         return false
  240.     else
  241.         return true
  242.     end
  243. end
  244.  
  245. local function cprint(txt)
  246.     local scr_x, scr_y = term.getSize()
  247.     local posX, posY = term.getCursorPos()
  248.     term.setCursorPos(math.ceil(scr_x/2)-math.ceil(#txt/2),posY)
  249.     return print(txt)
  250. end
  251.  
  252. local function writeConfig(password, overwrite)
  253.     if overwrite then
  254.         file = fs.open(passwordFile,"w")
  255.     else
  256.         file = fs.open(passwordFile,"a")
  257.     end
  258.     file.writeLine(password)
  259.     file.close()
  260. end
  261.  
  262. local function choice(filter, verbose)
  263.     if verbose == true then
  264.         write("[")
  265.         for a = 1, #filter do
  266.             write(string.upper(string.sub(filter,a,a)))
  267.             if a ~= #filter then
  268.                 write(",")
  269.             end
  270.         end
  271.         write("]?")
  272.     end
  273.     local event, key
  274.     repeat
  275.         event, key = os.pullEvent("char")
  276.     until string.find(filter,key)
  277.     if verbose then
  278.         write(string.upper(key))
  279.     end
  280.     return key, string.find(filter,key)
  281. end
  282.  
  283. local function correctPassword()
  284.     term.setBackgroundColor(palate.bgcorrect)
  285.     term.setTextColor(palate.txtcorrect)
  286.     local scr_x, scr_y = term.getSize()
  287.     term.setCursorPos(1,scr_y)
  288.     if #doorSide > 0 then
  289.         isCurrentlyGood = true
  290.     end
  291.     for a = 2, scr_y do
  292.         sleep(0)
  293.         term.scroll(1)
  294.         if a == math.ceil(scr_y/2) then
  295.             cprint("Correct!")
  296.         end
  297.     end
  298.     if #doorSide > 0 then
  299.         term.setCursorPos(1,math.floor(scr_y/2)+1)
  300.         cprint("Applying RS to "..table.concat(doorSide,", "))
  301.         for a = 1, #doorSide do
  302.             redstone.setOutput(doorSide[a], not doorInvertRedstone)
  303.         end
  304.         if type(doorCorrectDelay) == "number" then
  305.             sleep(doorCorrectDelay)
  306.         else
  307.             sleep(6)
  308.         end
  309.         isCurrentlyGood = false
  310.         for a = 1, #doorSide do
  311.             redstone.setOutput(doorSide[a], doorInvertRedstone)
  312.         end
  313.         return
  314.     else
  315.         sleep(0.6)
  316.         if _VERSION then
  317.             term.setCursorPos(1,scr_y/2)
  318.             term.setTextColor(colors.lightGray)
  319.             term.setBackgroundColor(colors.lightGray)
  320.             term.clear()
  321.             cprint("Correct!")
  322.             sleep(0)
  323.             term.setTextColor(colors.black)
  324.             term.setBackgroundColor(colors.gray)
  325.             term.clear()
  326.             sleep(0)
  327.         end
  328.         term.setBackgroundColor(colors.black)
  329.         term.clear()
  330.         term.setCursorPos(1,1)
  331.         return true
  332.     end
  333. end
  334.  
  335. local function incorrectPassword(pswd)
  336.     term.setBackgroundColor(palate.bgincorrect)
  337.     term.setTextColor(palate.txtincorrect)
  338.     local scr_x, scr_y = term.getSize()
  339.     term.clear()
  340.     term.setCursorPos(1,scr_y/2)
  341.     if string.lower(pswd) == "bepis" then
  342.         cprint("Bepis.")
  343.     else
  344.         cprint("WRONG.")
  345.     end
  346.     if type(doorIncorrectDelay) == "number" then
  347.         sleep(doorIncorrectDelay)
  348.     else
  349.         sleep(2)
  350.     end
  351. end
  352.  
  353. local function gui()
  354.     scr_x, scr_y = term.getSize()
  355.     while true do
  356.         term.setBackgroundColor(palate.bgcolor)
  357.         term.setTextColor(palate.bg_txtcolor)
  358.         term.clear()
  359.         term.setCursorPos(1,3)
  360.         cprint("Password:")
  361.         local posX, posY = term.getCursorPos()
  362.         term.setBackgroundColor(palate.fgcolor)
  363.         term.setTextColor(palate.fg_txtcolor)
  364.         local password
  365.         repeat
  366.             term.setCursorPos(1,posY)
  367.             term.clearLine()
  368.             write(">")
  369.             password = read("*")
  370.         until string.gsub(password," ","") ~= ""
  371.         local hashpassword = hash(password,saltCode)
  372.         term.setBackgroundColor(palate.fgcolor)
  373.         term.setTextColor(palate.fg_txtcolor)
  374.         local file = fs.open(passwordFile,"r")
  375.         local a, correct = nil, false
  376.         repeat
  377.             a = file.readLine()
  378.             if a == hashpassword then
  379.                 if correctPassword() == true then
  380.                     correct = true
  381.                     return true
  382.                 end
  383.             end
  384.         until not a
  385.         if not correct then
  386.             incorrectPassword(password)
  387.         end
  388.     end
  389. end
  390.  
  391. if not fs.exists(passwordFile) then
  392.     term.setBackgroundColor(palate.bgcolor)
  393.     term.setTextColor(palate.bg_txtcolor)
  394.     term.clear()
  395.     term.setCursorPos(1,2)
  396.     cprint("No password file!")
  397.     local newpassword, confpassword
  398.     repeat
  399.         repeat
  400.             cprint("Enter a new password.")
  401.             local posX, posY = term.getCursorPos()
  402.             term.setBackgroundColor(palate.fgcolor)
  403.             term.setTextColor(palate.fg_txtcolor)
  404.             repeat
  405.                 term.setCursorPos(1,posY)
  406.                 term.clearLine()
  407.                 write(">")
  408.                 newpassword = read("*")
  409.             until string.gsub(newpassword," ","") ~= ""
  410.             term.setBackgroundColor(palate.bgcolor)
  411.             term.setTextColor(palate.bg_txtcolor)
  412.            
  413.             cprint("Confirm that.")
  414.            
  415.             local posX, posY = term.getCursorPos()
  416.             term.setBackgroundColor(palate.fgcolor)
  417.             term.setTextColor(palate.fg_txtcolor)
  418.             repeat
  419.                 term.setCursorPos(1,posY)
  420.                 term.clearLine()
  421.                 write(">")
  422.                 confpassword = read("*")
  423.             until string.gsub(confpassword," ","") ~= ""
  424.             term.setBackgroundColor(palate.bgcolor)
  425.             term.setTextColor(palate.bg_txtcolor)
  426.             if newpassword ~= confpassword then
  427.                 cprint("They don't match!")
  428.                 local posX, posY = term.getCursorPos()
  429.                 posY = posY + 1
  430.             end
  431.         until newpassword == confpassword
  432.        
  433.         writeConfig(hash(newpassword,saltCode), false)
  434.         cprint("Wrote! Do another password?")
  435.         local input = choice("yn",true)
  436.     until input == "n"
  437.     term.setBackgroundColor(colors.black)
  438.     term.clear()
  439.     term.setCursorPos(1,1)
  440.     cprint("Got it, man.")
  441.     cprint("Run again to start the lock.")
  442.     return true
  443. end
  444.  
  445. while true do
  446.     status, res = pcall(gui)
  447.     if not status then
  448.         local a = handleCrash(res)
  449.         if a then
  450.             return true
  451.         end
  452.     else
  453.         break
  454.     end
  455. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement