Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local crypt = peripheral.wrap('left')
- local rfidw = peripheral.wrap('right')
- local rfidr = peripheral.wrap('top')
- local mag = peripheral.wrap('bottom')
- local tArgs = {...}
- local state = {
- magCard = false,
- rfidChip = false
- }
- local uName,
- uPass,
- uCipher,
- uKey,
- tEvent,
- tEvArg1,
- tEvArg2
- function readPass()
- local result = ''
- local x, y = term.getCursorPos()
- local e, k
- term.setCursorBlink(true)
- repeat
- e, k = os.pullEvent('key')
- if k > 1 and k < 12 and #result < 11 then
- k = ((k == 11 and 0) or k - 1)
- result = result..k
- elseif k == keys.backspace then
- result = result:sub(1, #result - 1)
- end
- term.clearLine()
- term.setCursorPos(1,y)
- for i=1, #result do
- term.write('*')
- end
- until k == keys.enter
- term.setCursorPos(1,y+1)
- term.setCursorBlink(false)
- return result
- end
- term.clear()
- term.setCursorPos(1,1)
- print('Please insert a username:')
- uName = read()
- local tPass
- local confirmed = false
- repeat
- term.clear()
- term.setCursorPos(1,1)
- print('Please insert a numeric password:')
- print('11 digits max')
- tPass = readPass()
- if #tPass > 0 then
- print('Please confirm the password:')
- uPass = readPass()
- if tPass == uPass then
- print('Password confirmed')
- confirmed = true
- else
- print('Password did not match')
- end
- else
- print('Password is required')
- end
- sleep(2)
- term.clear()
- term.setCursorPos(1,1)
- until confirmed
- rfidr.scan()
- while rfidr.isScanning() do
- tEvent, tEvArg1 = os.pullEvent()
- if tEvent == 'rfid_detected' then
- local uKeyEnc = tEvArg1
- print('Found existing encryption key')
- print('Use existing key?')
- print('enter: confirm')
- print('backspace: generate new')
- repeat
- tEvent, tEvArg1 = os.pullEvent('key')
- if tEvArg1 == keys.enter then
- uKey = crypt.decodeKey('aes', uKeyEnc)
- state.rfidChip = true
- break
- end
- until tEvArg1 == keys.backspace
- break
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- if not uKey then
- print('Generating key...')
- uKey = crypt.generateSymmetricKey('aes', 256)
- sleep(2)
- end
- print('Encrypting password...')
- uCipher = uKey.encrypt('aes', uPass)
- while not state.magCard do
- mag.beginWrite(uCipher, uName)
- mag.setInsertCardLight(true)
- print('Please swipe a mag card')
- tEvent, tEvArg1, tEvArg2 = os.pullEvent('mag_write_done')
- if #tEvArg2 > 0 then
- local magData = tEvArg2
- print('Data found on card:')
- print('enter: continue')
- print('r: restore')
- print('b: backup to local filesystem')
- while true do
- tEvent, tEvArg1 = os.pullEvent('key_up')
- if tEvArg1 == keys.enter then
- state.magCard = true
- break
- elseif tEvArg1 == keys.r then
- print('Lable cannot be restored,')
- print('please provide manually')
- mag.beginWrite(magData, read())
- print('Please swipe the card')
- os.pullEvent('mag_write_done')
- break
- elseif tEvArg1 == keys.b then
- if not fs.exists('backups') then
- fs.makeDir('backups')
- end
- local n = 1
- while fs.exists('backups/'..uName..n) do
- n = n + 1
- end
- print('Backing up to \'backups/'..uName..n..'\'')
- local backup = fs.open('backups/'..uName..n, 'w')
- backup.write(magData)
- backup.close()
- state.magCard = true
- break
- end
- end
- else
- state.magCard = true
- end
- mag.setInsertCardLight(false)
- end
- while not state.rfidChip do
- while not rfidw.isPresent() do
- print('Please insert a blank rfid chip into the writer')
- print('and press \'enter\' to continue')
- repeat
- tEvent, tEvArg1 = os.pullEvent('key_up')
- until tEvArg1 == keys.enter
- end
- if rfidw.isCoded() then
- print('The inserted rfid chip is already coded')
- print('replace it and press \'enter\' to continue')
- repeat
- tEvent, tEvArg1 = os.pullEvent('key_up')
- until tEvArg1 == keys.enter
- else
- print('Writing decryption key to rfid chip...')
- rfidw.encode(uKey.encode(), uName)
- local progress = rfidw.getProgress()
- local x, y
- while progress >= 0 do
- x, y = term.getCursorPos()
- term.clearLine()
- term.setCursorPos(1,y)
- term.write('Progress: '..math.floor((progress*100)+0.5)..'%')
- progress = rfidw.getProgress()
- sleep(1)
- end
- state.rfidChip = true
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- print('complete')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement