Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants for encryption and smart card
- local ENCRYPTION_KEY = 42 -- Simple XOR key for encryption (can be stronger)
- local SMART_CARD_FOLDER = "/disk/smartcards/"
- local SMART_CARD_EXT = ".sc"
- -- Encrypt a string using XOR encryption (simple method)
- local function encryptPin(pin)
- local encrypted = {}
- for i = 1, #pin do
- encrypted[i] = string.char(string.byte(pin, i) ~ ENCRYPTION_KEY)
- end
- return table.concat(encrypted)
- end
- -- Decrypt the PIN stored on the smart card
- local function decryptPin(encryptedPin)
- local decrypted = {}
- for i = 1, #encryptedPin do
- decrypted[i] = string.char(string.byte(encryptedPin, i) ~ ENCRYPTION_KEY)
- end
- return table.concat(decrypted)
- end
- -- Save smart card data (username, encrypted PIN) to a disk
- local function saveSmartCard(username, encryptedPin)
- local smartCardFile = fs.combine(SMART_CARD_FOLDER, username .. SMART_CARD_EXT)
- if fs.exists(smartCardFile) then
- fs.delete(smartCardFile) -- Overwrite existing smart card file
- end
- local file = fs.open(smartCardFile, "w")
- file.writeLine(encryptedPin)
- file.close()
- end
- -- Function to create a PIN and store it on the smart card
- local function createSmartCard(username)
- drawPopupWindow("Create a Smart Card", {"Enter a PIN for your smart card (4 digits):"})
- term.setCursorPos(1, select(2, term.getSize()))
- term.write("Enter PIN: ")
- local pin = read("*") -- Read PIN in masked format
- if #pin ~= PIN_LENGTH then
- drawPopupWindow("Error", {"PIN must be " .. PIN_LENGTH .. " digits."})
- os.sleep(2)
- return
- end
- -- Encrypt the PIN
- local encryptedPin = encryptPin(pin)
- -- Save to smart card
- saveSmartCard(username, encryptedPin)
- -- Success message
- drawPopupWindow("Smart Card Created", {"Your smart card has been created successfully!"})
- os.sleep(2)
- end
- -- Function to verify the smart card PIN during login
- local function verifySmartCard(username)
- local smartCardFile = fs.combine(SMART_CARD_FOLDER, username .. SMART_CARD_EXT)
- if not fs.exists(smartCardFile) then
- return false -- No smart card found
- end
- local file = fs.open(smartCardFile, "r")
- local encryptedPin = file.readLine()
- file.close()
- drawPopupWindow("Insert Smart Card", {"Please insert your smart card to continue..."})
- while true do
- local event, disk = os.pullEvent("disk")
- if event == "disk_insert" then
- if disk == SMART_CARD_FOLDER then
- -- Simulate smart card ID check (in reality, check disk ID here)
- local userInput = read("Enter PIN to authenticate: ")
- -- Decrypt and check PIN
- local decryptedPin = decryptPin(encryptedPin)
- if userInput == decryptedPin then
- return true
- else
- drawPopupWindow("Access Denied", {"Incorrect PIN!"})
- os.sleep(2)
- return false
- end
- end
- end
- end
- end
- -- Function to handle the setup and user creation for smart card authentication
- local function setupSmartCard(username)
- -- Start by setting up the smart card for the user
- drawPopupWindow("Smart Card Setup", {"You are in setup mode. Please create a PIN for your smart card."})
- -- Call the function to create the smart card
- createSmartCard(username)
- -- Remove setup file once completed
- fs.delete(SCSETUP_FILE)
- end
- -- Function to authenticate using a smart card (during login)
- local function insertSecurityCard(username)
- return verifySmartCard(username)
- end
- -- Main logic
- local function main()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- local username
- -- Check if in setup mode
- if checkSetupMode() then
- -- Handle smart card setup during setup mode
- drawPopupWindow("Setup Mode", {"System is in setup mode. Please configure your smart card."})
- local username = selectUserFromList()
- setupSmartCard(username) -- This will handle the smart card setup
- return
- end
- -- Check if AutoLogin is allowed
- local autoLoginUser = checkAutoLogin()
- if autoLoginUser then
- username = autoLoginUser
- drawPopupWindow("Autologin", {"Welcome back, " .. username .. "!"})
- os.sleep(2)
- else
- if fs.exists(SHOW_ALL_USERS_FILE) then
- username = selectUserFromList()
- else
- drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
- username = read()
- end
- end
- -- Attempt smart card login first
- if insertSecurityCard(username) then
- drawPopupWindow("Access Granted", {"Security card verified. Welcome, " .. username .. "!"})
- os.sleep(2)
- saveCurrentUser(username)
- shell.run("/disk/os/gui")
- return
- end
- -- Fallback to password login if card verification fails or is bypassed
- if checkCredentials(username) then
- drawPopupWindow("Access Granted", {"Welcome, " .. username .. "!"})
- os.sleep(2)
- saveCurrentUser(username)
- shell.run("/disk/os/gui")
- else
- shell.run("/disk/os/lock.lua")
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement