Skip to content

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

lua
base64encode(data: string) -> string
ParameterTypeDescription
datastringThe data to encode

Example

lua
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

lua
base64decode(data: string) -> string
ParameterTypeDescription
datastringThe Base64 encoded string

Example

lua
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.

lua
crypt.encrypt(data: string, key: string, iv: string?, mode: string?) -> (string, string)
ParameterTypeDefaultDescription
datastringThe unencoded content to encrypt
keystringA base64 encoded 256-bit key
ivstring?randomOptional base64 AES initialization vector
modestring?"CBC"The AES cipher mode

Cipher modes: CBC, ECB, CTR, CFB, OFB, GCM

Returns:

  • string — The base64 encoded encrypted data
  • string — The base64 encoded IV (generated if not provided)

Example

lua
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.

lua
crypt.decrypt(data: string, key: string, iv: string, mode: string?) -> string
ParameterTypeDefaultDescription
datastringThe base64 encoded encrypted content
keystringThe base64 encoded 256-bit key used to encrypt
ivstringThe base64 AES initialization vector
modestring?"CBC"The AES cipher mode used to encrypt

Cipher modes: CBC, ECB, CTR, CFB, OFB, GCM

Example

lua
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.

lua
crypt.hash(data: string, algorithm: string) -> string
ParameterTypeDescription
datastringThe unencoded content to hash
algorithmstringThe hash algorithm to use

Supported algorithms: sha1, sha256, sha384, sha512, md5, sha3-224, sha3-256, sha3-512

Example

lua
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.

lua
crypt.generatekey() -> string

Example

lua
local key = crypt.generatekey()
print("Key:", key)

crypt.generatebytes

Generates a string of random bytes.

lua
crypt.generatebytes(count: number) -> string
ParameterTypeDescription
countnumberNumber of random bytes to generate

Example

lua
local bytes = crypt.generatebytes(32)
print(#bytes) --> 32

LZ4 Compression

lz4compress

Compresses data using the LZ4 algorithm.

Aliases: lz4_compress

lua
lz4compress(data: string) -> string
ParameterTypeDescription
datastringThe data to compress

Example

lua
local data = string.rep("Hello! ", 1000)
local compressed = lz4compress(data)
print("Original:", #data, "Compressed:", #compressed)

lz4decompress

Decompresses LZ4 compressed data.

Aliases: lz4_decompress

lua
lz4decompress(data: string, original_size: number) -> string
ParameterTypeDescription
datastringThe compressed data
original_sizenumberThe original uncompressed size

Example

lua
local original = string.rep("Hello! ", 1000)
local compressed = lz4compress(original)
local decompressed = lz4decompress(compressed, #original)
print(decompressed == original) --> true