Advertisement
DOGGYWOOF

Untitled

Oct 27th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. -- Function to save the username and encrypted password to a file
  2. function saveCredentialsToFile(username, encryptedText, dir)
  3. local fileName = dir .. "/" .. username .. ".txt"
  4. local file = fs.open(fileName, "w")
  5. file.write(encryptedText)
  6. file.close()
  7. return fileName
  8. end
  9.  
  10. -- Modify the create function to save credentials in bootauth
  11. function create(username, text)
  12. local userDir = "/disk/users/" .. username
  13. local bootAuthDir = "/disk/bootauth"
  14.  
  15. if not fs.exists(bootAuthDir) then
  16. fs.makeDir(bootAuthDir)
  17. end
  18.  
  19. if not fs.exists(userDir) then
  20. fs.makeDir(userDir)
  21. end
  22.  
  23. local key = generateKey()
  24. local encryptedText = encrypt(text, key)
  25. local keyFileName = saveKeyToFile(key, userDir)
  26.  
  27. -- Save username and encrypted password in the bootauth directory
  28. local credentialsFileName = saveCredentialsToFile(username, encryptedText, bootAuthDir)
  29.  
  30. return { status = "success", encryptedText = encryptedText, keyFileName = keyFileName, credentialsFileName = credentialsFileName }
  31. end
  32.  
  33. -- Function to load the encrypted text from the bootauth directory
  34. function loadEncryptedTextFromBootAuth(username)
  35. local fileName = "/disk/bootauth/" .. username .. ".txt"
  36. if not fs.exists(fileName) then
  37. print("Credentials file does not exist.")
  38. return nil
  39. end
  40. local file = fs.open(fileName, "r")
  41. local encryptedText = file.readAll()
  42. file.close()
  43. return encryptedText
  44. end
  45.  
  46. -- Example Usage (remains the same)
  47. local tArgs = {...}
  48. if #tArgs < 1 then
  49. print("Usage: api <command> <args>")
  50. return
  51. end
  52.  
  53. local command = tArgs[1]
  54. if command == "create" then
  55. if #tArgs < 3 then
  56. print("Usage: api create <username> <text>")
  57. return
  58. end
  59. local username = tArgs[2]
  60. local text = table.concat(tArgs, " ", 3)
  61. local result = create(username, text)
  62. print(textutils.serialize(result))
  63. elseif command == "delete" then
  64. if #tArgs < 2 then
  65. print("Usage: api delete <username>")
  66. return
  67. end
  68. local username = tArgs[2]
  69. local result = delete(username)
  70. print(textutils.serialize(result))
  71. elseif command == "authenticate" then
  72. if #tArgs < 3 then
  73. print("Usage: api authenticate <adminUsername> <adminPassword>")
  74. return
  75. end
  76. local adminUsername = tArgs[2]
  77. local adminPassword = tArgs[3]
  78. local result = authenticate(adminUsername, adminPassword)
  79. print(textutils.serialize(result))
  80. else
  81. print("Invalid command. Use 'create', 'delete', or 'authenticate'.")
  82. end
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement