AES
The main Cryptography library provides core cryptographic functions for hashing, encryption, key generation, and more.
hash
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
Encrypts the input data using AES encryption.
Parameters
data
: The data to encryptkey
: 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
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
Generates a random key of the specified length (or 32 bytes if not specified).
Example
generatebytes
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
Computes an HMAC of the input data using the specified key and algorithm.
Parameters
key
: The secret keydata
: The data to authenticatealgorithm
: 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
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
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)