Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --CC:Tweaked
- --Dev:KiJDK
- local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
- --Simple Encryption
- function xorData(data, key)
- local result = {}
- local keyLen = #key
- for i = 1, #data do
- local dataByte = string.byte(data, i)
- local keyByte = string.byte(key, ((i - 1) % keyLen) + 1)
- table.insert(result, string.char(bit.bxor(dataByte, keyByte)))
- end
- return table.concat(result)
- end
- -- Encode a string to Base64
- function base64Encode(data)
- local result = ""
- local padding = ""
- while (#data % 3) ~= 0 do
- data = data .. "\0"
- padding = padding .. "="
- end
- for i = 1, #data, 3 do
- local b1 = string.byte(data, i)
- local b2 = string.byte(data, i+1)
- local b3 = string.byte(data, i+2)
- local n = b1 * 65536 + b2 * 256 + b3
- local c1 = math.floor(n / 262144) % 64 + 1
- local c2 = math.floor(n / 4096) % 64 + 1
- local c3 = math.floor(n / 64) % 64 + 1
- local c4 = n % 64 + 1
- result = result ..
- b64chars:sub(c1, c1) ..
- b64chars:sub(c2, c2) ..
- b64chars:sub(c3, c3) ..
- b64chars:sub(c4, c4)
- end
- return result:sub(1, #result - #padding) .. padding
- end
- -- Decode a Base64 string
- function base64Decode(data)
- data = data:gsub("=", "") -- remove padding
- local result = ""
- for i = 1, #data, 4 do
- local n = 0
- for j = 0, 3 do
- local c = data:sub(i+j, i+j)
- local index = b64chars:find(c, 1, true) or 0
- n = n * 64 + (index - 1)
- end
- local b1 = math.floor(n / 65536) % 256
- local b2 = math.floor(n / 256) % 256
- local b3 = n % 256
- result = result .. string.char(b1, b2, b3)
- end
- return result:gsub("%z+$", "") -- remove null padding bytes
- end
- --Simple Encryption
- --Other Helpers
- function split(str, sep)
- local result = {}
- sep = sep or "%s" -- default to splitting on whitespace
- for part in string.gmatch(str, "([^" .. sep .. "]+)") do
- table.insert(result, part)
- end
- return result
- end
- function copyToDisk(sourcePath, diskLabel, destPathOnDisk)
- -- Find the disk drive
- for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do
- if peripheral.getType(side) == "drive" then
- local mountPath = disk.getMountPath(side)
- if mountPath then
- if diskLabel == nil or disk.getLabel(side) == diskLabel then
- local destPath = fs.combine(mountPath, destPathOnDisk or fs.getName(sourcePath))
- -- Check if the file already exists
- if fs.exists(destPath) then
- --print("File already exists on disk: " .. destPath .. " Skipping copy.")
- return false
- else
- fs.copy(sourcePath, destPath)
- --print("Copied to: " .. destPath)
- return true
- end
- end
- end
- end
- end
- print("Disk not found or label mismatch.")
- return false
- end
- function deleteAllFilesOnDisk(diskLabel)
- -- Find the disk drive
- for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do
- if peripheral.getType(side) == "drive" then
- local mountPath = disk.getMountPath(side)
- if mountPath then
- if diskLabel == nil or disk.getLabel(side) == diskLabel then
- -- Recursively delete all files and folders
- deleteFilesRecursive(mountPath)
- --print("All files deleted from disk: " .. (diskLabel or "Unknown"))
- return true
- end
- end
- end
- end
- --print("Disk not found or label mismatch.")
- return false
- end
- -- Recursive function to delete files and folders
- function deleteFilesRecursive(path)
- for _, file in ipairs(fs.list(path)) do
- local fullPath = fs.combine(path, file)
- if fs.isDir(fullPath) then
- deleteFilesRecursive(fullPath) -- Recurse into subdirectory
- fs.delete(fullPath) -- Delete the directory after emptying it
- else
- fs.delete(fullPath) -- Delete the file
- end
- end
- end
- function checkComputerLabel(expectedLabel)
- local currentLabel = os.getComputerLabel()
- -- If the computer label doesn't match the expected one, exit the script
- if currentLabel ~= expectedLabel then
- --print("This script cannot run on this computer. Label mismatch.")
- return false
- end
- --print("Label matches: " .. currentLabel)
- return true
- end
- --Other Helpers
- --Password Check
- function checkPassword(expectedPassword)
- -- Prompt user for password
- write("Enter password: ")
- local userInput = read("*") -- read password input without showing it on screen
- -- Check if the entered password matches the expected password
- if userInput == expectedPassword then
- print("Password correct.")
- return true
- else
- print("Incorrect password.")
- return false
- end
- end
- --
- --Hashing Helpers
- function simpleHash(data)
- local hash = 0
- for i = 1, #data do
- hash = bit.bxor(hash, string.byte(data, i) * i)
- hash = (hash * 31) % 4294967296 -- keep it in 32-bit range
- end
- return string.format("%08x", hash)
- end
- -- Function to hash a file's content
- function hashFile(filePath)
- local file = fs.open(filePath, "r")
- if not file then
- print("File not found: " .. filePath)
- return nil
- end
- local fileContent = file.readAll()
- file.close()
- return simpleHash(fileContent)
- end
- -- Function to check a file's hash against a known hash
- function checkFileHash(filePath, expectedHash)
- local computedHash = hashFile(filePath)
- if computedHash then
- if computedHash == expectedHash then
- --print("Hash matches!")
- return true
- else
- --print("Hash does not match.")
- return false
- end
- end
- return false
- end
- --Hashing Helpers
Comments
-
- -- Required Imports
- local libs = require("libs")
- --
Add Comment
Please, Sign In to add comment