GhastlyDoMinic

Computer Craft Tweaked _ libs.lua

Apr 8th, 2025 (edited)
47
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.27 KB | None | 0 0
  1. --CC:Tweaked
  2. --Dev:KiJDK
  3.  
  4. local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  5.  
  6. --Simple Encryption
  7. function xorData(data, key)
  8.     local result = {}
  9.     local keyLen = #key
  10.  
  11.     for i = 1, #data do
  12.         local dataByte = string.byte(data, i)
  13.         local keyByte = string.byte(key, ((i - 1) % keyLen) + 1)
  14.         table.insert(result, string.char(bit.bxor(dataByte, keyByte)))
  15.     end
  16.  
  17.     return table.concat(result)
  18. end
  19.  
  20. -- Encode a string to Base64
  21. function base64Encode(data)
  22.     local result = ""
  23.     local padding = ""
  24.  
  25.     while (#data % 3) ~= 0 do
  26.         data = data .. "\0"
  27.         padding = padding .. "="
  28.     end
  29.  
  30.     for i = 1, #data, 3 do
  31.         local b1 = string.byte(data, i)
  32.         local b2 = string.byte(data, i+1)
  33.         local b3 = string.byte(data, i+2)
  34.  
  35.         local n = b1 * 65536 + b2 * 256 + b3
  36.  
  37.         local c1 = math.floor(n / 262144) % 64 + 1
  38.         local c2 = math.floor(n / 4096) % 64 + 1
  39.         local c3 = math.floor(n / 64) % 64 + 1
  40.         local c4 = n % 64 + 1
  41.  
  42.         result = result ..
  43.             b64chars:sub(c1, c1) ..
  44.             b64chars:sub(c2, c2) ..
  45.             b64chars:sub(c3, c3) ..
  46.             b64chars:sub(c4, c4)
  47.     end
  48.  
  49.     return result:sub(1, #result - #padding) .. padding
  50. end
  51.  
  52. -- Decode a Base64 string
  53. function base64Decode(data)
  54.     data = data:gsub("=", "") -- remove padding
  55.  
  56.     local result = ""
  57.     for i = 1, #data, 4 do
  58.         local n = 0
  59.         for j = 0, 3 do
  60.             local c = data:sub(i+j, i+j)
  61.             local index = b64chars:find(c, 1, true) or 0
  62.             n = n * 64 + (index - 1)
  63.         end
  64.  
  65.         local b1 = math.floor(n / 65536) % 256
  66.         local b2 = math.floor(n / 256) % 256
  67.         local b3 = n % 256
  68.  
  69.         result = result .. string.char(b1, b2, b3)
  70.     end
  71.  
  72.     return result:gsub("%z+$", "") -- remove null padding bytes
  73. end
  74. --Simple Encryption
  75.  
  76. --Other Helpers
  77. function split(str, sep)
  78.     local result = {}
  79.     sep = sep or "%s"  -- default to splitting on whitespace
  80.  
  81.     for part in string.gmatch(str, "([^" .. sep .. "]+)") do
  82.         table.insert(result, part)
  83.     end
  84.  
  85.     return result
  86. end
  87.  
  88. function copyToDisk(sourcePath, diskLabel, destPathOnDisk)
  89.     -- Find the disk drive
  90.     for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do
  91.         if peripheral.getType(side) == "drive" then
  92.             local mountPath = disk.getMountPath(side)
  93.             if mountPath then
  94.                 if diskLabel == nil or disk.getLabel(side) == diskLabel then
  95.                     local destPath = fs.combine(mountPath, destPathOnDisk or fs.getName(sourcePath))
  96.                    
  97.                     -- Check if the file already exists
  98.                     if fs.exists(destPath) then
  99.                         --print("File already exists on disk: " .. destPath .. " Skipping copy.")
  100.                         return false
  101.                     else
  102.                         fs.copy(sourcePath, destPath)
  103.                         --print("Copied to: " .. destPath)
  104.                         return true
  105.                     end
  106.                 end
  107.             end
  108.         end
  109.     end
  110.     print("Disk not found or label mismatch.")
  111.     return false
  112. end
  113.  
  114. function deleteAllFilesOnDisk(diskLabel)
  115.     -- Find the disk drive
  116.     for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do
  117.         if peripheral.getType(side) == "drive" then
  118.             local mountPath = disk.getMountPath(side)
  119.             if mountPath then
  120.                 if diskLabel == nil or disk.getLabel(side) == diskLabel then
  121.                     -- Recursively delete all files and folders
  122.                     deleteFilesRecursive(mountPath)
  123.                     --print("All files deleted from disk: " .. (diskLabel or "Unknown"))
  124.                     return true
  125.                 end
  126.             end
  127.         end
  128.     end
  129.     --print("Disk not found or label mismatch.")
  130.     return false
  131. end
  132.  
  133. -- Recursive function to delete files and folders
  134. function deleteFilesRecursive(path)
  135.     for _, file in ipairs(fs.list(path)) do
  136.         local fullPath = fs.combine(path, file)
  137.         if fs.isDir(fullPath) then
  138.             deleteFilesRecursive(fullPath)  -- Recurse into subdirectory
  139.             fs.delete(fullPath)  -- Delete the directory after emptying it
  140.         else
  141.             fs.delete(fullPath)  -- Delete the file
  142.         end
  143.     end
  144. end
  145.  
  146. function checkComputerLabel(expectedLabel)
  147.     local currentLabel = os.getComputerLabel()
  148.  
  149.     -- If the computer label doesn't match the expected one, exit the script
  150.     if currentLabel ~= expectedLabel then
  151.         --print("This script cannot run on this computer. Label mismatch.")
  152.         return false
  153.     end
  154.    
  155.     --print("Label matches: " .. currentLabel)
  156.     return true
  157. end
  158. --Other Helpers
  159.  
  160. --Password Check
  161. function checkPassword(expectedPassword)
  162.     -- Prompt user for password
  163.     write("Enter password: ")
  164.     local userInput = read("*")  -- read password input without showing it on screen
  165.  
  166.     -- Check if the entered password matches the expected password
  167.     if userInput == expectedPassword then
  168.         print("Password correct.")
  169.         return true
  170.     else
  171.         print("Incorrect password.")
  172.         return false
  173.     end
  174. end
  175. --
  176.  
  177. --Hashing Helpers
  178. function simpleHash(data)
  179.     local hash = 0
  180.     for i = 1, #data do
  181.         hash = bit.bxor(hash, string.byte(data, i) * i)
  182.         hash = (hash * 31) % 4294967296 -- keep it in 32-bit range
  183.     end
  184.     return string.format("%08x", hash)
  185. end
  186.  
  187. -- Function to hash a file's content
  188. function hashFile(filePath)
  189.     local file = fs.open(filePath, "r")
  190.     if not file then
  191.         print("File not found: " .. filePath)
  192.         return nil
  193.     end
  194.     local fileContent = file.readAll()
  195.     file.close()
  196.     return simpleHash(fileContent)
  197. end
  198.  
  199. -- Function to check a file's hash against a known hash
  200. function checkFileHash(filePath, expectedHash)
  201.     local computedHash = hashFile(filePath)
  202.     if computedHash then
  203.         if computedHash == expectedHash then
  204.             --print("Hash matches!")
  205.             return true
  206.         else
  207.             --print("Hash does not match.")
  208.             return false
  209.         end
  210.     end
  211.     return false
  212. end
  213. --Hashing Helpers
Comments
Add Comment
Please, Sign In to add comment