Cryptographic primitives for hashing, encryption, and encoding
Provides SHA-256/512 hashing, HMAC, AES-128-ECB encryption, base64 and hex encoding/decoding, and secure random number generation. Used internally for MeshCore channel encryption and message authentication. All operations use hardware-accelerated mbedTLS where available.
| Parameter | Description |
|---|---|
| key | 16-byte key (must match encryption key) |
| ciphertext | Data to decrypt (must be multiple of 16 bytes) |
local key = ez.crypto.derive_channel_key("my secret")
local encrypted = ez.crypto.aes128_ecb_encrypt(key, "hello")
local decrypted = ez.crypto.aes128_ecb_decrypt(key, encrypted)
print(decrypted:gsub("%z+$", "")) -- Trim trailing zeros
| Parameter | Description |
|---|---|
| key | 16-byte key (use derive_channel_key to create from password) |
| plaintext | Data to encrypt (will be zero-padded to block boundary) |
local key = ez.crypto.derive_channel_key("my secret")
local encrypted = ez.crypto.aes128_ecb_encrypt(key, "hello")
print("Encrypted:", ez.crypto.bytes_to_hex(encrypted))
| Parameter | Description |
|---|---|
| encoded | Base64 encoded string |
local decoded = ez.crypto.base64_decode("SGVsbG8sIFdvcmxkIQ==")
print(decoded) -- "Hello, World!"
| Parameter | Description |
|---|---|
| data | Binary string to encode |
local encoded = ez.crypto.base64_encode("Hello, World!")
print(encoded) -- "SGVsbG8sIFdvcmxkIQ=="
| Parameter | Description |
|---|---|
| data | Binary string |
local hash = ez.crypto.sha256("test")
print(ez.crypto.bytes_to_hex(hash))
-- Shows 64-character hex string
| Parameter | Description |
|---|---|
| key | 16-byte channel key |
local key = ez.crypto.public_channel_key()
local hash = ez.crypto.channel_hash(key)
print("Public channel hash:", hash)
| Parameter | Description |
|---|---|
| input | Password or channel name string |
local key = ez.crypto.derive_channel_key("SecretChannel2024")
print("Key:", ez.crypto.bytes_to_hex(key))
local hash = ez.crypto.channel_hash(key)
print("Channel hash:", hash)
| Parameter | Description |
|---|---|
| hex | Hex string (case-insensitive, must have even length) |
local key = ez.crypto.hex_to_bytes("8b3387e9c5cdea6ac9e5edbaa115cd72")
print("Key length:", #key) -- 16 bytes
| Parameter | Description |
|---|---|
| key | Binary string key (any length, but 32 bytes recommended) |
| data | Binary string to authenticate |
local key = ez.crypto.random_bytes(32)
local mac = ez.crypto.hmac_sha256(key, "message to authenticate")
-- Verify by recomputing and comparing
local verify = ez.crypto.hmac_sha256(key, "message to authenticate")
if mac == verify then print("Authentic") end
local public_key = ez.crypto.public_channel_key()
print("Public key:", ez.crypto.bytes_to_hex(public_key))
-- Output: 8b3387e9c5cdea6ac9e5edbaa115cd72
| Parameter | Description |
|---|---|
| count | Number of bytes to generate (1-256) |
local key = ez.crypto.random_bytes(16) -- Generate 128-bit key
local nonce = ez.crypto.random_bytes(12) -- 96-bit nonce
print("Key:", ez.crypto.bytes_to_hex(key))
| Parameter | Description |
|---|---|
| data | Binary string to hash |
local hash = ez.crypto.sha256("hello world")
print("SHA256:", ez.crypto.bytes_to_hex(hash))
-- Output: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
| Parameter | Description |
|---|---|
| data | Binary string to hash |
local hash = ez.crypto.sha512("secret data")
print("SHA512:", ez.crypto.bytes_to_hex(hash))