Skip to content

AES

The main Cryptography library provides core cryptographic functions for hashing, encryption, key generation, and more.


hash

function crypt.hash(data: string, algorithm: string): string

Computes a hash of the input data using the specified algorithm.

Example

local hashed = crypt.hash("Hello World", "sha256")
print(hashed) -- Outputs the SHA-256 hash of "Hello World"

encrypt

function crypt.encrypt(data: string, key: string, iv: string?, mode: string?): (string, string)

Encrypts the input data using AES encryption.

Parameters

  • data: The data to encrypt
  • key: The encryption key (base64 encoded)
  • iv: Initialization vector (optional, base64 encoded)
  • mode: Encryption mode (optional, defaults to "CBC")

Returns

  • Encrypted data (base64 encoded)
  • IV used (base64 encoded)

Example

local key = crypt.generatekey(32)
local encrypted, iv = crypt.encrypt("Secret message", key)
print(encrypted, iv)

decrypt

function crypt.decrypt(data: string, key: string, iv: string, mode: string): string

Decrypts AES encrypted data.

Parameters

  • data: The encrypted data (base64 encoded)
  • key: The decryption key (base64 encoded)
  • iv: Initialization vector (base64 encoded)
  • mode: Decryption mode

Example

local decrypted = crypt.decrypt(encrypted, key, iv, "CBC")
print(decrypted) -- Should output "Secret message"

generatekey

function crypt.generatekey(length: number?): string

Generates a random key of the specified length (or 32 bytes if not specified).

Example

local key = crypt.generatekey(16)
print(key) -- Outputs a base64 encoded 16-byte random key

generatebytes

function crypt.generatebytes(count: number?): string

Generates random bytes of the specified count (or 16 if not specified).

Example

local random_bytes = crypt.generatebytes(24)
print(random_bytes) -- Outputs base64 encoded 24 random bytes

hmac

function crypt.hmac(key: string, data: string, algorithm: string): string

Computes an HMAC of the input data using the specified key and algorithm.

Parameters

  • key: The secret key
  • data: The data to authenticate
  • algorithm: Hash algorithm to use ("sha256" or "sha512")

Example

local hmac_result = crypt.hmac("secret_key", "data_to_authenticate", "sha256")
print(hmac_result) -- Outputs the base64 encoded HMAC

wrap_key

function crypt.wrap_key(kek: string, key: string): string

Wraps a key using AES Key Wrap algorithm.

Parameters

  • kek: Key Encryption Key (16, 24, or 32 bytes)
  • key: The key to wrap

Example

local wrapped_key = crypt.wrap_key(kek, key_to_wrap)
print(wrapped_key) -- Outputs the base64 encoded wrapped key

unwrap_key

function crypt.unwrap_key(kek: string, wrapped_key: string): string

Unwraps a key that was wrapped using AES Key Wrap algorithm.

Parameters

  • kek: Key Encryption Key (must be the same used for wrapping)
  • wrapped_key: The wrapped key (base64 encoded)

Example

local unwrapped_key = crypt.unwrap_key(kek, wrapped_key)
print(unwrapped_key) -- Outputs the base64 encoded unwrapped key