Advertisement
DOGGYWOOF

Encrypted user creation API

Jul 15th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 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. -- Example Usage
  139. local tArgs = {...}
  140. if #tArgs < 1 then
  141. print("Usage: api <command> <args>")
  142. return
  143. end
  144.  
  145. local command = tArgs[1]
  146. if command == "create" then
  147. if #tArgs < 3 then
  148. print("Usage: api create <username> <text>")
  149. return
  150. end
  151. local username = tArgs[2]
  152. local text = table.concat(tArgs, " ", 3)
  153. local result = create(username, text)
  154. print(textutils.serialize(result))
  155. elseif command == "delete" then
  156. if #tArgs < 2 then
  157. print("Usage: api delete <username>")
  158. return
  159. end
  160. local username = tArgs[2]
  161. local result = delete(username)
  162. print(textutils.serialize(result))
  163. elseif command == "authenticate" then
  164. if #tArgs < 3 then
  165. print("Usage: api authenticate <adminUsername> <adminPassword>")
  166. return
  167. end
  168. local adminUsername = tArgs[2]
  169. local adminPassword = tArgs[3]
  170. local result = authenticate(adminUsername, adminPassword)
  171. print(textutils.serialize(result))
  172. else
  173. print("Invalid command. Use 'create', 'delete', or 'authenticate'.")
  174. end
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement