Mag card door V1.0

Discussion in 'ComputerCraft Programming' started by Azimath, Aug 15, 2013.

  1. Azimath

    Azimath Master Zepplin Thief

    Joined:
    Jan 22, 2012
    Messages:
    30
    Likes Received:
    6
    So i found the mag card readers we have, and decided to play with them and a bit of crypto to make a door that requires a magnetic card to be swiped.

    Here's how it works:
    1. An admin card is swiped, which has a hardcoded password (stored as a hash in the computer).
    2. The computer accepts the card if it is an admin card and prompts the user to insert a card.
    3. The computer writes a random 15 character password to the card, and stores an SHA1 hashed version in its memory, creating a door card.
    4. When a door card is swiped, the door opens for 3 seconds (under optimal TPS, if this matters).
    Here's the hardware needed:
    1. A door closed by redstone.
    2. A computer, advanced or regular.
    3. A monitor and mag card reader connected by wired modems to the computer
    Optionally, you can have readers away from the monitor to have more than one reader open the door. Only the first reader connected will work to make cards. This is because the computer gets a "mag_swipe" event from any reader connected, and checks the card against the stored hashes. However it needs to wrap the peripheral to actually write, plus the monitor nearby gives confirmation of a valid admin card. The monitor display text needs fixing to line wrap properly, but whatever.

    Hardware pics:

    [​IMG]
    Main area

    [​IMG]
    Door open

    [​IMG]
    Inside of door (lever keeps door closed if down, overriding cards)

    [​IMG]
    Some wiring/peripherals

    [​IMG]
    Another view of wiring.

    Heres the code:
    Code:
    os.loadAPI("SHA")
    os.pullEvent = os.pullEventRaw
     
    redstone.setBundledOutput("left", colors.white)
    math.randomseed(os.time())
     
    term.clear()
    term.setCursorPos(1,1)
    print("Legoman Technologies door lock V1.0")
    print("Thank you to tomas1666 for his SHA1 code")
     
    modem = peripheral.wrap("top")
     
    if modem == nil then
      error("Modem not on top")
    end
     
    if modem.isPresentRemote("mag card reader_0") then
      reader = peripheral.wrap("mag card reader_0")
      print("Card reader connected")
    else
      error("Mag-card reader not found")
    end
     
    if modem.isPresentRemote("monitor_0") then
      monitor = peripheral.wrap("monitor_0")
      print("Monitor connected")
    else
      error("Monitor not found")
    end
     
    hashedPw = "Put the hashed admin password here"
    cardsFilePath = "Cards"
    cards = {""}
    chars = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","0","1","2","3","4","5","6","7","8","9", "@", "#", "$", "%", "&", "?"}
    newpass = ""
    cardNum = 0
     
    if fs.exists(cardsFilePath) then
      cardsFile = fs.open(cardsFilePath, "r")
      cards = textutils.unserialize(cardsFile.readAll())
      cardsFile.close()
    end
     
    while true do
      admin = false
     
      monitor.clear()
      monitor.setCursorPos(1,1)
      monitor.write("Welcome to Legoman Industries! Please insert a card!")
      reader.setInsertCardLight(true)
     
     
      event, p1,p2,p3 = os.pullEvent()
     
      if event == "mag_swipe" then
        if SHA.SHA1_2(p1) == hashedPw then
          admin = true
        end
        for x = 1, #cards do
          if SHA.SHA1_2(p1) == cards[x] then
            reader.setInsertCardLight(false)
            redstone.setBundledOutput("left", 0)
            os.sleep(3)
            redstone.setBundledOutput("left", colors.white)
            reader.setInsertCardLight(true)
            break
          end
        end   
      end
      if admin == true then
        reader.setInsertCardLight(false)
        monitor.clear()
        monitor.setCursorPos(1,1)
        monitor.write("Admin granted! Insert blank card!")
        for z = 1,15 do
          case = math.random(1,2) -- randomly choose case (caps or lower)
          a = math.random(1,#chars) -- randomly choose a character from the "char" array
          if case == 1 then
            x=string.upper(chars[a]) -- uppercase if case = 1
          elseif case == 2 then
            x=string.lower(chars[a]) -- lowercase if case = 2
          end
          newpass = newpass..x
        end
        reader.setInsertCardLight(true)
        cardNum = #cards
        print(reader.beginWrite(newpass, cardNum..""))
        table.insert(cards, SHA.SHA1_2(newpass))
       
        while reader.isWaiting() do
        end
       
        reader.setInsertCardLight(false)
        monitor.clear()
        monitor.setCursorPos(1,1)
        monitor.write("New card created!")
        newpass = ""
      end
     
      os.sleep(1)
     
      cardsFile = fs.open(cardsFilePath, "w")
      cardsFile.write(textutils.serialize(cards))
      cardsFile.close()
     
     
    end
    I'll probably edit this post tomorrow when i have time, and fix the text on the screen to look better.
     
  2. Azimath

    Azimath Master Zepplin Thief

    Joined:
    Jan 22, 2012
    Messages:
    30
    Likes Received:
    6
    Well edit post is broken, and i wanted to note that there's a computercraft bug where you may need to reconnect the peripherals from time to time by right clicking on the defective peripheral's modem twice.
     
  3. Neonbeta

    Neonbeta Person who did stuff and things

    Joined:
    Mar 2, 2012
    Messages:
    2,603
    Likes Received:
    757
    I hope you have warded glass near to prevent the usage of enderpearls to get through the door. But nice nonetheless!
     
  4. fxstriker

    fxstriker Good Bye skcraft.

    Joined:
    Jan 14, 2012
    Messages:
    259
    Likes Received:
    35
    Shadow raises a good point
    high tech locking system ....
    *Hole in door .....
     
  5. Azimath

    Azimath Master Zepplin Thief

    Joined:
    Jan 22, 2012
    Messages:
    30
    Likes Received:
    6
    If i had the time, Id make the door solid and do one of those crazy piston contraptions. Keeps the mobs out at least. It doesn't do much for our base against players due to the numerous jet-pack exits in our ceilings.
     
    Shadow likes this.
  6. fxstriker

    fxstriker Good Bye skcraft.

    Joined:
    Jan 14, 2012
    Messages:
    259
    Likes Received:
    35
    Why not look into the drawbridge block;
    it is part of tinkers construct.
     
  7. ghostpotato

    ghostpotato The Crimson King

    Joined:
    Jan 27, 2013
    Messages:
    92
    Likes Received:
    6
    Looks nice. How do you use rednet cables with the computer.
     
  8. Azimath

    Azimath Master Zepplin Thief

    Joined:
    Jan 22, 2012
    Messages:
    30
    Likes Received:
    6
    You use the bundled cable functions in the redstone API. It works the same way redpower bundled cable does.
     
  9. Siioh

    Siioh If you can read this, you're too far

    Joined:
    May 29, 2013
    Messages:
    333
    Likes Received:
    118
    Ya know, if for mobs you can use ethereal glass from Extra Utilities...but that's boring :D
     
  10. Azimath

    Azimath Master Zepplin Thief

    Joined:
    Jan 22, 2012
    Messages:
    30
    Likes Received:
    6
    The reason I've done anything impractical.
    After some server trouble wiped the code from the computer (I could get it from the post), and the pistons disappeared, and the wired cable kept bugging out, Ive stopped using the computer system, and gone to a TC drawbridge and lever thing to close the "lobby" (where my enhanced portal is) off from the rest of my base.
     
  11. LeiserGeist

    LeiserGeist Gamer, Software Developer

    Joined:
    Oct 26, 2012
    Messages:
    479
    Likes Received:
    116
    I like your system of storing hashed passwords in the computer.
    Maybe in the future if you have more than one of these, you could use rednet to encrypt and send them to a master computer or server. That would be cool.


    I made a similar system, although it doesn't use passwords, but it uses security clearance levels (which are based off MJ12, a "secret police" in an old game's story)
    • Each "agent" has a card listing their name & security level encrypted and decrypted using a master key.
    • Each area in my compound that uses this system has a different required clearance, for example, level zero is Seraphic/8X, the command center is Throne/6G, etc.
    • If someone with a greater or equal security clearance swipes their card in the door, it will open and greet them with a text to speech peripheral, "Welcome to (area), Agent (name)."
    • New cards are made in the security office, requiring the base commander with Seraphic/8X clearance to create them.
    • Each area has their own access log, including attempts to crash the system (flooding, in which case it simply reboots)
    • Pretty pointless for a compound consisting of one person... but I had fun making it >__>
    • Not going to release it yet, maybe after I completely finish it.
    Not trying to hijack your topic or anything. I'll put this somewhere else if you want, Legoman.
     
  12. Azimath

    Azimath Master Zepplin Thief

    Joined:
    Jan 22, 2012
    Messages:
    30
    Likes Received:
    6
    That sounds pretty cool. It sounds like it would be pretty hard for you to revoke a lost/stolen card's permissions it sounds like (since some of us like to pretend we have agents/employees/subjugates that need to go in and out all the time and might lose a card). I would have mine running to a master server if I hadn't been playing star-made so much recently. My base isn't complex enough (It consists of a power wing, an automation wing, and a manual crafting/monitoring room) for me to want to implement a multilevel permission system (yet), especially since I usually fly out the "jetpack entrance" rather than mucking about with the door .
     
  13. LeiserGeist

    LeiserGeist Gamer, Software Developer

    Joined:
    Oct 26, 2012
    Messages:
    479
    Likes Received:
    116
    Heh, honestly I didn't think of that. I might make a master-server system for storing users and their clearance levels..
     
  14. Magi1053

    Magi1053 That one guy
    Staff Member

    Joined:
    Jun 26, 2012
    Messages:
    159
    Likes Received:
    49
    You could use a player detector to detect if the player that is swiping the card matches the player the card belongs to.
     
  15. LeiserGeist

    LeiserGeist Gamer, Software Developer

    Joined:
    Oct 26, 2012
    Messages:
    479
    Likes Received:
    116
    Well that ruins my point of it being realistic xD
     
  16. Magi1053

    Magi1053 That one guy
    Staff Member

    Joined:
    Jun 26, 2012
    Messages:
    159
    Likes Received:
    49
    It would be kind of a tossup between being realistic, yet still secure. They would still need the access card for it to work
     
  17. Pathaleon

    Pathaleon Forum & Server Administrator
    Staff Member

    Joined:
    Jul 3, 2012
    Messages:
    1,014
    Likes Received:
    670
    Well, realistically if you stole a mag card from somebody it would open the door regardless of who you are. The mag cards on minecraft are more of a fun than secure thing imo.
     
  18. LeiserGeist

    LeiserGeist Gamer, Software Developer

    Joined:
    Oct 26, 2012
    Messages:
    479
    Likes Received:
    116
    exactly