Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/PrinceOfCookies/CookieOS/llms.txt

Use this file to discover all available pages before exploring further.

cosUtils ships a self-contained cryptography and data-utility layer built entirely in Lua for use inside CC:Tweaked. It provides LZW compression (compress/decompress), RC4 stream cipher encryption (used internally), MD5 integrity hashing (also internal), hex encoding helpers, a character-interleaving helper, and a high-level Package/UnPackage pipeline that chains all three primitives together.
These utilities are designed for convenience in a Minecraft ComputerCraft environment, not for production security. RC4 has well-known cryptographic weaknesses and LZW has no authentication by itself. Do not rely on them to protect genuinely sensitive data outside the game.

cosUtils.compress(input, ignoreSize)

Compresses a string using an LZW-based algorithm. If the compressed output turns out to be the same size or larger than the input (and ignoreSize is not true), the function returns the original string with a "u" prefix to signal that it is stored uncompressed. Successfully compressed data is returned with a "c" prefix. Both prefixes are understood by cosUtils.decompress().
input
string
The string to compress. Must be a string; passing any other type returns nil and an error message.
ignoreSize
boolean
default:"false"
If true, the compressed output is returned even if it is larger than the input (i.e. always returns the "c"-prefixed compressed form).
Returns compressedString (string) on success, or nil, errorMessage (string) on failure.
local compressed = cosUtils.compress("hello world hello world")
print(compressed:sub(1, 1)) -- "c" (compressed) or "u" (uncompressed)

-- Force compression even if output is larger
local forced = cosUtils.compress("hi", true)
print(forced:sub(1, 1)) -- always "c"

cosUtils.decompress(input)

Reverses cosUtils.compress(). Reads the first byte to determine whether the data is compressed ("c") or stored raw ("u") and processes accordingly. Returns nil and an error message if the input is missing the control prefix or is otherwise malformed.
input
string
A string previously returned by cosUtils.compress(). Must begin with "c" or "u".
Returns decompressedString (string) on success, or nil, errorMessage (string) on failure.
local original = "hello world hello world"
local compressed = cosUtils.compress(original)
local recovered = cosUtils.decompress(compressed)
print(recovered == original) -- true

cosUtils.Package(KEY, data)

High-level encryption pipeline. Encrypts data with the RC4 stream cipher using KEY, compresses the encrypted result with LZW, appends a 16-byte MD5 hash of KEY .. compressedData for integrity verification, and returns the finished payload.
KEY
string
Encryption key. For correct RC4 operation the key must be exactly 16 bytes. The function asserts this length requirement internally.
data
string
The plaintext string to encrypt and package.
Returns the packaged string.
local key = "mysecretkey12345"  -- exactly 16 bytes
local packaged = cosUtils.Package(key, "sensitive data")
-- Result: compress(rc4(key, data)) .. md5(key .. compressed)

cosUtils.isPackaged(KEY, data)

Verifies whether data was packaged with KEY by checking the appended MD5 hash. First attempts a full-data check (last 16 bytes as hash), then iterates through possible split points to handle segmented data.
KEY
string
The key that was used to call cosUtils.Package().
data
string
The packaged data string to verify.
Returns isValid (boolean), compressedData (string | nil). On success compressedData is the verified compressed payload (without the hash suffix) ready to pass to cosUtils.decompress().
local key = "mysecretkey12345"
local packaged = cosUtils.Package(key, "hello")

local valid, compressed = cosUtils.isPackaged(key, packaged)
print(valid)      -- true
print(compressed) -- the compressed blob (without hash)

-- Wrong key
local invalid, _ = cosUtils.isPackaged("wrongkey1234567", packaged)
print(invalid)    -- false

cosUtils.UnPackage(KEY, data)

Reverses the full Package() pipeline. Processes data in 16-byte segments, calling cosUtils.isPackaged() on each segment to find valid chunks. For each valid segment, it decompresses the payload and accumulates the results. Returns the concatenated plaintext of all valid segments.
KEY
string
The key originally used with cosUtils.Package().
data
string
The packaged data string returned by cosUtils.Package().
Returns decryptedString (string) on success, or nil, errorMessage (string) if no valid data was found.
local key = "mysecretkey12345"
local packaged = cosUtils.Package(key, "secret message")
local recovered, err = cosUtils.UnPackage(key, packaged)
print(recovered) -- "secret message"

cosUtils.interleave(data1, data2)

Interleaves two strings character by character: data1[1], data2[1], data1[2], data2[2], …. If the strings are different lengths, the remaining characters of the longer string are appended at the end.
data1
string
First input string.
data2
string
Second input string.
Returns the interleaved string.
local result = cosUtils.interleave("abc", "123")
print(result) -- "a1b2c3"

-- Unequal lengths
local result2 = cosUtils.interleave("abcd", "12")
print(result2) -- "a1b2cd"

cosUtils.fromhex(str)

Converts a hexadecimal string (pairs of hex digits) into a binary string by parsing each two-character group as a byte value.
str
string
A hex string with an even number of characters, e.g. "48656C6C6F".
Returns the decoded binary string.
local bin = cosUtils.fromhex("48656C6C6F")
print(bin) -- "Hello"

cosUtils.tohex(str)

Converts a binary string into an uppercase hexadecimal string. Each byte is formatted as two uppercase hex digits.
str
string
Any binary or ASCII string to encode.
Returns the hex-encoded string.
local hex = cosUtils.tohex("Hello")
print(hex) -- "48656C6C6F"

End-to-end Package/UnPackage example

local key = "mykey16byteslong"  -- must be exactly 16 bytes
local original = "This is secret data"

-- Encrypt and package
local packaged = cosUtils.Package(key, original)

-- Verify integrity before unpacking
local valid, compressed = cosUtils.isPackaged(key, packaged)
if valid then
    print("Data integrity verified")
end

-- Full unpack (verify + decompress + decrypt)
local recovered, err = cosUtils.UnPackage(key, packaged)
if recovered then
    print(recovered) -- "This is secret data"
else
    print("Unpack failed: " .. err)
end

Build docs developers (and LLMs) love