Advertisement
DOGGYWOOF

User creation for Doggy OS

Jul 15th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.69 KB | None | 0 0
  1. -- Function to generate a key for encryption and decryption
  2. function generateKey()
  3. local chars = {}
  4. for i = 32, 126 do -- ASCII range for printable characters
  5. table.insert(chars, string.char(i))
  6. end
  7.  
  8. local key = {}
  9. while #chars > 0 do
  10. local index = math.random(#chars)
  11. table.insert(key, table.remove(chars, index))
  12. end
  13.  
  14. return table.concat(key)
  15. end
  16.  
  17. -- Function to create a map from a key
  18. function createMap(key)
  19. local map = {}
  20. for i = 32, 126 do
  21. map[string.char(i)] = key:sub(i - 31, i - 31)
  22. end
  23. return map
  24. end
  25.  
  26. -- Function to invert a map
  27. function invertMap(map)
  28. local invMap = {}
  29. for k, v in pairs(map) do
  30. invMap[v] = k
  31. end
  32. return invMap
  33. end
  34.  
  35. -- Function to encrypt text using a key
  36. function encrypt(text, key)
  37. local map = createMap(key)
  38. local encrypted = {}
  39. for i = 1, #text do
  40. local char = text:sub(i, i)
  41. table.insert(encrypted, map[char] or char)
  42. end
  43. return table.concat(encrypted)
  44. end
  45.  
  46. -- Function to decrypt text using a key
  47. function decrypt(text, key)
  48. local map = invertMap(createMap(key))
  49. local decrypted = {}
  50. for i = 1, #text do
  51. local char = text:sub(i, i)
  52. table.insert(decrypted, map[char] or char)
  53. end
  54. return table.concat(decrypted)
  55. end
  56.  
  57. -- Function to save the key to a file
  58. function saveKeyToFile(key, dir)
  59. local randomNumber = math.random(1, 1000)
  60. local fileName = dir .. "/key" .. randomNumber .. ".txt"
  61. local file = fs.open(fileName, "w")
  62. file.write(key)
  63. file.close()
  64. return fileName
  65. end
  66.  
  67. -- Function to load the key from a file
  68. function loadKeyFromFile(fileName)
  69. local file = fs.open(fileName, "r")
  70. local key = file.readAll()
  71. file.close()
  72. return key
  73. end
  74.  
  75. -- Function to find the key file in a directory
  76. function findKeyFile(dir)
  77. local files = fs.list(dir)
  78. for _, file in ipairs(files) do
  79. if file:match("^key%d+%.txt$") then
  80. return dir .. "/" .. file
  81. end
  82. end
  83. return nil
  84. end
  85.  
  86. -- Function to load the encrypted text from the password file
  87. function loadEncryptedText(userDir)
  88. local fileName = userDir .. "/password.txt"
  89. if not fs.exists(fileName) then
  90. print("Password file does not exist.")
  91. return nil
  92. end
  93. local file = fs.open(fileName, "r")
  94. local encryptedText = file.readAll()
  95. file.close()
  96. return encryptedText
  97. end
  98.  
  99. -- API Functions
  100. function create(username, text)
  101. local userDir = "/disk/users/" .. username
  102.  
  103. if not fs.exists(userDir) then
  104. fs.makeDir(userDir)
  105. end
  106.  
  107. local key = generateKey()
  108. local encryptedText = encrypt(text, key)
  109. local keyFileName = saveKeyToFile(key, userDir)
  110.  
  111. local passwordFile = fs.open(userDir .. "/password.txt", "w")
  112. passwordFile.write(encryptedText)
  113. passwordFile.close()
  114.  
  115. return { status = "success", encryptedText = encryptedText, keyFileName = keyFileName }
  116. end
  117.  
  118. function delete(username)
  119. local userDir = "/disk/users/" .. username
  120.  
  121. if fs.exists(userDir) then
  122. fs.delete(userDir)
  123. return { status = "success", message = "User directory deleted." }
  124. else
  125. return { status = "error", message = "User directory not found." }
  126. end
  127. end
  128.  
  129. function authenticate(adminUsername, adminPassword)
  130. -- Replace with actual admin credential verification
  131. if adminUsername == "admin" and adminPassword == "password" then
  132. return { status = "success", message = "Authenticated successfully." }
  133. else
  134. return { status = "error", message = "Authentication failed." }
  135. end
  136. end
  137.  
  138. -- Function to create a new user
  139. function createUser()
  140. term.clear()
  141. term.setCursorPos(1, 1)
  142. print("Enter new username:")
  143. local username = read()
  144.  
  145. -- Check if the username already exists
  146. if fs.exists("/disk/users/" .. username) then
  147. print("Username already exists. Try again.")
  148. sleep(2)
  149. return
  150. end
  151.  
  152. -- Create a directory for the new user
  153. fs.makeDir("/disk/users/" .. username)
  154.  
  155. -- Prompt for password
  156. local password
  157. repeat
  158. term.clear()
  159. term.setCursorPos(1, 1)
  160. print("Enter password:")
  161. password = read("*")
  162.  
  163. print("Confirm password:")
  164. local confirmPassword = read("*")
  165.  
  166. if password ~= confirmPassword then
  167. print("Passwords do not match. Try again.")
  168. sleep(2)
  169. end
  170. until password == confirmPassword
  171.  
  172. -- Encrypt the password before storing
  173. local key = generateKey()
  174. local encryptedPassword = encrypt(password, key)
  175.  
  176. -- Store the encrypted password in a text file
  177. local file = fs.open("/disk/users/" .. username .. "/password.txt", "w")
  178. file.write(encryptedPassword)
  179. file.close()
  180.  
  181. -- Save the encryption key to a file
  182. saveKeyToFile(key, "/disk/users/" .. username)
  183.  
  184. print("User created successfully.")
  185. sleep(2)
  186. end
  187.  
  188. -- Function to delete a user
  189. function deleteUser()
  190. term.clear()
  191. term.setCursorPos(1, 1)
  192. print("Enter username to delete:")
  193. local username = read()
  194.  
  195. -- Check if the username exists
  196. if not fs.exists("/disk/users/" .. username) then
  197. print("User not found. Try again.")
  198. sleep(2)
  199. return
  200. end
  201.  
  202. -- Prompt for password to confirm deletion
  203. print("Enter password to confirm deletion:")
  204. local inputPassword = read("*")
  205.  
  206. -- Read encryption key from file
  207. local keyFileName = findKeyFile("/disk/users/" .. username)
  208. if not keyFileName then
  209. print("Encryption key not found. Deletion failed.")
  210. sleep(2)
  211. return
  212. end
  213. local key = loadKeyFromFile(keyFileName)
  214.  
  215. -- Read stored encrypted password from the file
  216. local file = fs.open("/disk/users/" .. username .. "/password.txt", "r")
  217. local encryptedPassword = file.readAll()
  218. file.close()
  219.  
  220. -- Decrypt stored password and compare with input password
  221. local storedPassword = decrypt(encryptedPassword, key)
  222. if inputPassword == storedPassword then
  223. -- Delete the user directory
  224. fs.delete("/disk/users/" .. username)
  225. print("User deleted successfully.")
  226. else
  227. print("Incorrect password. Deletion failed.")
  228. end
  229.  
  230. sleep(2)
  231. end
  232.  
  233. -- Function for password recovery by admin
  234. function passwordRecovery()
  235. term.clear()
  236. term.setCursorPos(1, 1)
  237. print("Enter admin username:")
  238. local adminUsername = read()
  239.  
  240. -- Check if the entered user has admin privileges
  241. if not fs.exists("/disk/users/" .. adminUsername .. "/admin.txt") then
  242. print("Permission denied. Admin access required.")
  243. sleep(2)
  244. return
  245. end
  246.  
  247. -- Prompt for admin password
  248. print("Enter admin password:")
  249. local adminPassword = read("*")
  250.  
  251. -- Read stored admin password from the file
  252. local adminFile = fs.open("/disk/users/" .. adminUsername .. "/password.txt", "r")
  253. local storedAdminPassword = adminFile.readAll()
  254. adminFile.close()
  255.  
  256. -- Decrypt stored admin password and compare with input admin password
  257. local keyFileName = findKeyFile("/disk/users/" .. adminUsername)
  258. if not keyFileName then
  259. print("Encryption key not found. Password reset failed.")
  260. sleep(2)
  261. return
  262. end
  263. local key = loadKeyFromFile(keyFileName)
  264. local decryptedAdminPassword = decrypt(storedAdminPassword, key)
  265.  
  266. if adminPassword == decryptedAdminPassword then
  267. print("Enter username for password reset:")
  268. local username = read()
  269.  
  270. -- Check if the username exists
  271. if not fs.exists("/disk/users/" .. username) then
  272. print("User not found. Try again.")
  273. sleep(2)
  274. return
  275. end
  276.  
  277. -- Confirm password reset
  278. print("Are you sure you want to reset the password for " .. username .. "? (y/n)")
  279. local confirm = read()
  280. if confirm:lower() ~= "y" then
  281. print("Password reset canceled.")
  282. sleep(2)
  283. return
  284. end
  285.  
  286. -- Prompt for new password
  287. term.clear()
  288. term.setCursorPos(1, 1)
  289. print("Enter new password for " .. username .. ":")
  290. local newPassword = read("*")
  291.  
  292. -- Encrypt the new password before storing
  293. local newKey = generateKey()
  294. local encryptedNewPassword = encrypt(newPassword, newKey)
  295.  
  296. -- Store the encrypted new password in the text file
  297. local file = fs.open("/disk/users/" .. username .. "/password.txt", "w")
  298. file.write(encryptedNewPassword)
  299. file.close()
  300.  
  301. -- Save the encryption key for the new password
  302. saveKeyToFile(newKey, "/disk/users/" .. username)
  303.  
  304. print("Password reset successfully.")
  305. else
  306. print("Incorrect admin password. Password reset failed.")
  307. end
  308.  
  309. sleep(2)
  310. end
  311.  
  312. -- Function to exit the program
  313. function exitProgram()
  314. term.clear()
  315. term.setCursorPos(1, 1)
  316. print("Exiting...")
  317. sleep(2)
  318. term.clear()
  319. term.setCursorPos(1, 1)
  320. os.shutdown()
  321. end
  322.  
  323. -- Function to list all users (directories) in /disk/users
  324. function viewAllUsers()
  325. term.clear()
  326. term.setCursorPos(1, 1)
  327. print("List of Users:")
  328. local users = fs.list("/disk/users")
  329. for _, user in ipairs(users) do
  330. print("- " .. user)
  331. end
  332. print("\nPress any key to return to the main menu.")
  333. os.pullEvent("key")
  334. end
  335.  
  336. -- Function to run Security Card Setup
  337. function runSecurityCardSetup()
  338. term.clear()
  339. term.setCursorPos(1, 1)
  340. shell.run("/disk/security/CardSetup")
  341. print("Security Card Setup completed. Press any key to continue.")
  342. os.pullEvent("key")
  343. end
  344.  
  345. -- Function to display the main menu
  346. function mainMenu()
  347. while true do
  348. term.clear()
  349. term.setCursorPos(1, 1)
  350. print("Main Menu:")
  351. print("1. Create User")
  352. print("2. Delete User")
  353. print("3. Password Recovery")
  354. print("4. View All Users")
  355. print("5. Security Card Login")
  356. print("6. Exit")
  357.  
  358. local choice = read()
  359.  
  360. if choice == "1" then
  361. createUser()
  362. elseif choice == "2" then
  363. deleteUser()
  364. elseif choice == "3" then
  365. passwordRecovery()
  366. elseif choice == "4" then
  367. viewAllUsers()
  368. elseif choice == "5" then
  369. runSecurityCardSetup()
  370. elseif choice == "6" then
  371. exitProgram()
  372. else
  373. print("Invalid choice. Try again.")
  374. sleep(2)
  375. end
  376. end
  377. end
  378.  
  379. -- Main program entry point
  380. mainMenu()
  381.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement