Cryptography
Functions for encoding, decoding, encrypting, and hashing data. Includes the crypt library, base64 library, and LZ4 compression.
Base64
base64encode
Encodes a string to Base64.
Aliases: base64_encode, encodebase64, base64.encode, crypt.base64encode, crypt.base64_encode, crypt.base64.encode
base64encode(data: string) -> string| Parameter | Type | Description |
|---|---|---|
data | string | The data to encode |
Example
local encoded = base64encode("Hello, World!")
print(encoded) --> "SGVsbG8sIFdvcmxkIQ=="base64decode
Decodes a Base64 string back to its original form.
Aliases: base64_decode, base64.decode, crypt.base64decode, crypt.base64_decode, crypt.base64.decode
base64decode(data: string) -> string| Parameter | Type | Description |
|---|---|---|
data | string | The Base64 encoded string |
Example
local decoded = base64decode("SGVsbG8sIFdvcmxkIQ==")
print(decoded) --> "Hello, World!"Crypt Library
The crypt library provides encryption, decryption, hashing, and key generation functions.
crypt.encrypt
Encrypts an unencoded string using AES encryption. Returns the base64 encoded encrypted string and the IV.
If an AES IV is not provided, a random one will be generated and returned as a second base64 encoded string.
crypt.encrypt(data: string, key: string, iv: string?, mode: string?) -> (string, string)| Parameter | Type | Default | Description |
|---|---|---|---|
data | string | — | The unencoded content to encrypt |
key | string | — | A base64 encoded 256-bit key |
iv | string? | random | Optional base64 AES initialization vector |
mode | string? | "CBC" | The AES cipher mode |
Cipher modes: CBC, ECB, CTR, CFB, OFB, GCM
Returns:
string— The base64 encoded encrypted datastring— The base64 encoded IV (generated if not provided)
Example
local key = crypt.generatekey()
local encrypted, iv = crypt.encrypt("secret message", key)
print(encrypted, iv)
-- With explicit IV and mode
local encrypted2, iv2 = crypt.encrypt("secret message", key, iv, "CTR")crypt.decrypt
Decrypts base64 encoded and encrypted content. Returns the raw decrypted string.
crypt.decrypt(data: string, key: string, iv: string, mode: string?) -> string| Parameter | Type | Default | Description |
|---|---|---|---|
data | string | — | The base64 encoded encrypted content |
key | string | — | The base64 encoded 256-bit key used to encrypt |
iv | string | — | The base64 AES initialization vector |
mode | string? | "CBC" | The AES cipher mode used to encrypt |
Cipher modes: CBC, ECB, CTR, CFB, OFB, GCM
Example
local key = crypt.generatekey()
local encrypted, iv = crypt.encrypt("secret message", key)
local decrypted = crypt.decrypt(encrypted, key, iv)
print(decrypted) --> "secret message"
-- With explicit mode
local decrypted2 = crypt.decrypt(encrypted, key, iv, "CBC")crypt.hash
Hashes data using the specified algorithm.
crypt.hash(data: string, algorithm: string) -> string| Parameter | Type | Description |
|---|---|---|
data | string | The unencoded content to hash |
algorithm | string | The hash algorithm to use |
Supported algorithms: sha1, sha256, sha384, sha512, md5, sha3-224, sha3-256, sha3-512
Example
local hash = crypt.hash("Hello, World!", "md5")
print(hash) --> "65A8E27D8879283831B664BD8B7F0AD4"
local sha256 = crypt.hash("Hello, World!", "sha256")
print(sha256)crypt.generatekey
Generates a random encryption key.
crypt.generatekey() -> stringExample
local key = crypt.generatekey()
print("Key:", key)crypt.generatebytes
Generates a string of random bytes.
crypt.generatebytes(count: number) -> string| Parameter | Type | Description |
|---|---|---|
count | number | Number of random bytes to generate |
Example
local bytes = crypt.generatebytes(32)
print(#bytes) --> 32LZ4 Compression
lz4compress
Compresses data using the LZ4 algorithm.
Aliases: lz4_compress
lz4compress(data: string) -> string| Parameter | Type | Description |
|---|---|---|
data | string | The data to compress |
Example
local data = string.rep("Hello! ", 1000)
local compressed = lz4compress(data)
print("Original:", #data, "Compressed:", #compressed)lz4decompress
Decompresses LZ4 compressed data.
Aliases: lz4_decompress
lz4decompress(data: string, original_size: number) -> string| Parameter | Type | Description |
|---|---|---|
data | string | The compressed data |
original_size | number | The original uncompressed size |
Example
local original = string.rep("Hello! ", 1000)
local compressed = lz4compress(original)
local decompressed = lz4decompress(compressed, #original)
print(decompressed == original) --> true