Advertisement
Guest User

registerUser

a guest
Jul 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. local crypt = peripheral.wrap('left')
  2. local rfidw = peripheral.wrap('right')
  3. local rfidr = peripheral.wrap('top')
  4. local mag = peripheral.wrap('bottom')
  5. local tArgs = {...}
  6. local state = {
  7.   magCard = false,
  8.   rfidChip = false
  9. }
  10.  
  11. local uName,
  12.       uPass,
  13.       uCipher,
  14.       uKey,
  15.       tEvent,
  16.       tEvArg1,
  17.       tEvArg2
  18.  
  19. function readPass()
  20.   local result = ''
  21.   local x, y = term.getCursorPos()
  22.   local e, k
  23.  
  24.   term.setCursorBlink(true)  
  25.   repeat
  26.     e, k = os.pullEvent('key')
  27.     if k > 1 and k < 12 and #result < 11 then
  28.       k = ((k == 11 and 0) or k - 1)
  29.       result = result..k
  30.     elseif k == keys.backspace then
  31.       result = result:sub(1, #result - 1)
  32.     end
  33.    
  34.     term.clearLine()
  35.     term.setCursorPos(1,y)
  36.     for i=1, #result do
  37.       term.write('*')
  38.     end
  39.   until k == keys.enter
  40.   term.setCursorPos(1,y+1)
  41.   term.setCursorBlink(false)
  42.  
  43.   return result
  44. end
  45.  
  46. term.clear()
  47. term.setCursorPos(1,1)
  48. print('Please insert a username:')
  49. uName = read()
  50.  
  51. local tPass
  52. local confirmed = false
  53.    
  54. repeat
  55.   term.clear()
  56.   term.setCursorPos(1,1)
  57.   print('Please insert a numeric password:')
  58.   print('11 digits max')
  59.   tPass = readPass()
  60.   if #tPass > 0 then
  61.     print('Please confirm the password:')
  62.     uPass = readPass()
  63.     if tPass == uPass then
  64.       print('Password confirmed')
  65.       confirmed = true
  66.     else
  67.       print('Password did not match')
  68.     end
  69.   else
  70.     print('Password is required')
  71.   end
  72.   sleep(2)
  73.   term.clear()
  74.   term.setCursorPos(1,1)
  75. until confirmed
  76.  
  77. rfidr.scan()
  78. while rfidr.isScanning() do
  79.   tEvent, tEvArg1 = os.pullEvent()
  80.   if tEvent == 'rfid_detected' then
  81.     local uKeyEnc = tEvArg1
  82.     print('Found existing encryption key')
  83.     print('Use existing key?')
  84.     print('enter: confirm')
  85.     print('backspace: generate new')
  86.     repeat
  87.       tEvent, tEvArg1 = os.pullEvent('key')
  88.       if tEvArg1 == keys.enter then
  89.         uKey = crypt.decodeKey('aes', uKeyEnc)
  90.         state.rfidChip = true
  91.         break
  92.       end
  93.     until tEvArg1 == keys.backspace
  94.     break
  95.   end
  96. end
  97.  
  98. term.clear()
  99. term.setCursorPos(1,1)
  100. if not uKey then
  101.   print('Generating key...')
  102.   uKey = crypt.generateSymmetricKey('aes', 256)
  103.   sleep(2)
  104. end
  105.  
  106. print('Encrypting password...')
  107. uCipher = uKey.encrypt('aes', uPass)
  108.  
  109. while not state.magCard do
  110.   mag.beginWrite(uCipher, uName)
  111.   mag.setInsertCardLight(true)
  112.   print('Please swipe a mag card')
  113.   tEvent, tEvArg1, tEvArg2 = os.pullEvent('mag_write_done')
  114.   if #tEvArg2 > 0 then
  115.     local magData = tEvArg2
  116.  
  117.     print('Data found on card:')
  118.     print('enter: continue')
  119.     print('r: restore')
  120.     print('b: backup to local filesystem')
  121.  
  122.     while true do
  123.       tEvent, tEvArg1 = os.pullEvent('key_up')
  124.    
  125.       if tEvArg1 == keys.enter then
  126.         state.magCard = true
  127.         break
  128.       elseif tEvArg1 == keys.r then
  129.         print('Lable cannot be restored,')
  130.         print('please provide manually')
  131.         mag.beginWrite(magData, read())
  132.         print('Please swipe the card')
  133.         os.pullEvent('mag_write_done')
  134.         break
  135.       elseif tEvArg1 == keys.b then
  136.         if not fs.exists('backups') then
  137.           fs.makeDir('backups')
  138.         end
  139.        
  140.         local n = 1
  141.         while fs.exists('backups/'..uName..n) do
  142.           n = n + 1
  143.         end
  144.         print('Backing up to \'backups/'..uName..n..'\'')
  145.         local backup = fs.open('backups/'..uName..n, 'w')
  146.         backup.write(magData)
  147.         backup.close()
  148.         state.magCard = true
  149.         break
  150.       end
  151.     end
  152.   else
  153.     state.magCard = true
  154.   end
  155.  
  156.   mag.setInsertCardLight(false)
  157. end
  158.  
  159. while not state.rfidChip do
  160.   while not rfidw.isPresent() do
  161.     print('Please insert a blank rfid chip into the writer')
  162.     print('and press \'enter\' to continue')
  163.     repeat
  164.       tEvent, tEvArg1 = os.pullEvent('key_up')
  165.     until tEvArg1 == keys.enter
  166.   end
  167.    
  168.   if rfidw.isCoded() then
  169.     print('The inserted rfid chip is already coded')
  170.     print('replace it and press \'enter\' to continue')
  171.     repeat
  172.       tEvent, tEvArg1 = os.pullEvent('key_up')
  173.     until tEvArg1 == keys.enter
  174.   else
  175.     print('Writing decryption key to rfid chip...')
  176.     rfidw.encode(uKey.encode(), uName)
  177.     local progress = rfidw.getProgress()
  178.     local x, y
  179.     while progress >= 0 do
  180.       x, y = term.getCursorPos()
  181.       term.clearLine()
  182.       term.setCursorPos(1,y)
  183.       term.write('Progress: '..math.floor((progress*100)+0.5)..'%')
  184.       progress = rfidw.getProgress()
  185.       sleep(1)
  186.     end
  187.    
  188.     state.rfidChip = true
  189.   end
  190. end
  191.  
  192. term.clear()
  193. term.setCursorPos(1,1)
  194. print('complete')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement