SecureTable
The SecureTable library provides functionality for creating secure and protected tables in Lua. It offers features such as memory protection, encryption, and integrity verification to ensure the safety of the stored data. This isn't perfect, simply an obfuscation technique to hide your data. It also doubles as a way to manage your data in a nice way.
SecureTable.new(narray, nhash)
Creates a new secure table with the specified array and hash sizes.
narray
: The size of the array part of the table (default: 33).nhash
: The size of the hash part of the table (default: 17).
Returns a new secure table object.
Secure Table Methods
secureTable:Set(key, value)
Sets a key-value pair in the secure table.
key
: The key to set (can be a string or number).value
: The value to associate with the key.
secureTable:Get(key)
Retrieves the value associated with the specified key from the secure table.
key
: The key to retrieve the value for (can be a string or number).
Returns the value associated with the key, or nil
if the key doesn't exist.
secureTable:Clear()
Clears all key-value pairs from the secure table.
secureTable:Size()
Returns the size of the array part of the secure table.
Returns the size of the array part.
secureTable:Lock()
Locks the secure table, making it read-only and preventing further modifications.
secureTable:Unlock()
Unlocks the secure table, allowing modifications again.
secureTable:Protect(size)
Protects a region of memory with the specified size in the secure table.
size
: The size of the memory region to protect.
secureTable:Verify()
Verifies the integrity of the secure table and its protected regions.
local isValid, error = secureTable:Verify()
if not isValid then
print("Secure table integrity compromised:", error)
end
Returns two values:
1. A boolean indicating if the secure table is valid and intact.
2. An error message string if the integrity is compromised (empty if valid).
secureTable:Wipe()
Wipes the secure table, clearing all data and releasing memory.
secureTable:Stats()
Retrieves statistics about the secure table's memory usage.
local stats = secureTable:Stats()
print("Total allocated:", stats.total_allocated)
print("Array used:", stats.array_used)
print("Hash used:", stats.hash_used)
print("Protected size:", stats.protected_size)
Returns a table with the following fields:
- total_allocated
: The total memory allocated for the secure table.
- array_used
: The number of used slots in the array part.
- hash_used
: The number of used slots in the hash part.
- protected_size
: The total size of protected memory regions.
Examples
Creating and using a secure table
local secureTable = SecureTable.new(100, 50)
secureTable:Set("name", "John")
secureTable:Set("age", 30)
secureTable:Set(1, "First item")
local name = secureTable:Get("name")
local age = secureTable:Get("age")
local firstItem = secureTable:Get(1)
print("Name:", name)
print("Age:", age)
print("First item:", firstItem)
Locking and unlocking a secure table
local secureTable = SecureTable.new(100, 50)
secureTable:Set("key", "value")
secureTable:Lock()
-- Attempting to modify the table will raise an error
secureTable:Set("another_key", "another_value")
secureTable:Unlock()
-- Modification is allowed again
secureTable:Set("another_key", "another_value")