Advertisement
LDDestroier

Hashed KEYCARD Lock (CC)

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