TinyAES

LicenseBuild Status

TinyAES is a lightweight, dependency-free Elixir wrapper for AES-256-GCM encryption and decryption using Erlang's :crypto module. It provides robust error handling, support for Additional Authenticated Data (AAD), and a simple API. The encryption key is securely retrieved from the ENCRYPTION_KEY environment variable.

Features

Installation

Add TinyAES to your mix.exs dependencies:

{:tiny_aes, "~> 0.1"}

Run mix deps.get to fetch the dependency.

Setup

Generate a 32-byte encryption key, and add it to your .env file:

mix run -e 'TinyAES.puts_generate_key_env()'
# Copy the output to .env:
ENCRYPTION_KEY=your_base64_encoded_key_here

The function outputs a base64-encoded key, e.g., OuaO+dtNNgxJjQLGHMLJ9m8rSQDsVdqkGrf7ySSj3Yg=. Add the key to your .env file:

Ensure the ENCRYPTION_KEY environment variable is set in your application (e.g., using env_loader or dotenv).

Usage

Encrypt and decrypt data without optional AAD::

# Encrypt a message
plaintext = "Sensitive data"
ciphertext = TinyAES.encrypt(plaintext)

# Decrypt it
{:ok, decrypted} = TinyAES.decrypt(ciphertext)
assert decrypted == plaintext

Encrypt and decrypt data with optional AAD (e.g., user or session ID)

# Encrypt a message
plaintext = "Sensitive data"
ciphertext = TinyAES.encrypt(plaintext, "optional_aad")

# Decrypt it
{:ok, decrypted} = TinyAES.decrypt(ciphertext, "optional_aad")
assert decrypted == plaintext

Handle edge cases:

# Handle invalid input
TinyAES.decrypt("not a binary")
# {:error, "Ciphertext must be a binary with at least 32 bytes"}

# Handle missing key
System.delete_env("ENCRYPTION_KEY")
TinyAES.encrypt("test")
# {:error, "Encryption key not found in environment variable ENCRYPTION_KEY"}

API

See HexDocs for full documentation.

Contributing

Contributions are welcome! Please open an issue or pull request on GitHub. For major changes, discuss them in an issue first. Ensure tests pass with mix test.

License

Released under the MIT License. See the LICENSE file for details.

Acknowledgments

Developed with the help of Grok, created by xAI. Thanks to Erlang's :crypto for providing a solid foundation.