Cryptographic utilities — hashing, symmetric/asymmetric encryption, KDF.
Provides a unified interface for common cryptographic operations:
- Hashing: SHA-256, SHA-512, MD5, HMAC
- Symmetric authenticated: AES-256-GCM, ChaCha20-Poly1305
- Symmetric streaming: AES-256-CTR (for large files/streams)
- Key exchange: ECDH (X25519)
- Key derivation: PBKDF2, Argon2id
- Asymmetric: RSA key generation
- Random generation: hex, token, password
- Utilities: secure_compare, generate_key
All keys and IVs use :crypto.strong_rand_bytes/1. Encrypted values are
self-contained (IV/nonce + tag + ciphertext, Base64-encoded).
Summary
Functions
Derives a key using Argon2id (requires optional argon2_elixir dependency).
Computes a shared secret from your private key and peer's public key.
Decrypts a value encrypted with encrypt/2. Returns {:ok, plaintext} or {:error, reason}.
Decrypts ChaCha20-Poly1305 encrypted data.
Decrypts data encrypted with AES-256-CTR streaming.
Encrypts plaintext with AES-256-GCM. Returns {:ok, ciphertext}.
Encrypts plaintext with ChaCha20-Poly1305.
Generates an X25519 key pair. Returns {private_key, public_key} (both raw binary).
Generates a random 256-bit key.
Generates an RSA key pair (2048-bit). Returns {private_der, public_der}.
HMAC-SHA256 (hex encoded).
MD5 hash (hex encoded). NOTE: MD5 is cryptographically broken — only for checksums.
Derives a key using PBKDF2-HMAC-SHA256.
Generates a random hex string.
Generates a random password with configurable length and character sets.
Generates a random URL-safe token.
Timing-safe string comparison.
SHA-256 hash (hex encoded).
SHA-512 hash (hex encoded).
Encrypts a chunk of data in streaming mode.
Finalizes a streaming encryption. Returns the final state (discard after).
Starts an AES-256-CTR encryption stream. Use with stream_encrypt/2 and stream_finalize/1.
Functions
Derives a key using Argon2id (requires optional argon2_elixir dependency).
Computes a shared secret from your private key and peer's public key.
Decrypts a value encrypted with encrypt/2. Returns {:ok, plaintext} or {:error, reason}.
Decrypts ChaCha20-Poly1305 encrypted data.
Decrypts data encrypted with AES-256-CTR streaming.
Encrypts plaintext with AES-256-GCM. Returns {:ok, ciphertext}.
Encrypts plaintext with ChaCha20-Poly1305.
Generates an X25519 key pair. Returns {private_key, public_key} (both raw binary).
@spec generate_key() :: binary()
Generates a random 256-bit key.
Generates an RSA key pair (2048-bit). Returns {private_der, public_der}.
HMAC-SHA256 (hex encoded).
MD5 hash (hex encoded). NOTE: MD5 is cryptographically broken — only for checksums.
Derives a key using PBKDF2-HMAC-SHA256.
@spec random_hex(non_neg_integer()) :: binary()
Generates a random hex string.
@spec random_password( non_neg_integer(), keyword() ) :: binary()
Generates a random password with configurable length and character sets.
@spec random_token(non_neg_integer()) :: binary()
Generates a random URL-safe token.
Timing-safe string comparison.
SHA-256 hash (hex encoded).
SHA-512 hash (hex encoded).
Encrypts a chunk of data in streaming mode.
Finalizes a streaming encryption. Returns the final state (discard after).
Starts an AES-256-CTR encryption stream. Use with stream_encrypt/2 and stream_finalize/1.