Back to Documentation
API Reference
⚙️Encryption API Reference
Siro provides AES-256 encryption with HKDF key separation and Encrypt-then-MAC authentication.
Overview#
Siro provides AES-256 encryption with HKDF key separation and Encrypt-then-MAC authentication.
php
use Siro\Core\Encrypter;
Configuration#
env
APP_KEY=your-32-character-app-key-here
Generate a key:
bash
php siro key:generate
Basic Usage#
Encrypt#
php
$encrypted = Encrypter::encrypt('sensitive-data');
// Returns base64-encoded string with IV + ciphertext + MAC
Decrypt#
php
$decrypted = Encrypter::decrypt($encrypted);
// Returns original plaintext string
How It Works#
Encryption Flow#
- Generate random 16-byte IV (Initialization Vector)
- Derive encryption key using HKDF-SHA256
- Encrypt plaintext with AES-256-CBC
- Compute HMAC-SHA256 over IV + ciphertext
- Return base64(IV + ciphertext + MAC)
Decryption Flow#
- Decode base64 input
- Extract IV, ciphertext, MAC
- Verify MAC using
hash_equals(timing-safe) - Decrypt with AES-256-CBC
- Return plaintext
Key Separation#
The master key (APP_KEY) is never used directly for encryption. Instead, HKDF derives separate keys:
| Purpose | Derived Key |
|---|---|
| Encryption | HKDF(APP_KEY, "encryption") |
| Authentication | HKDF(APP_KEY, "authentication") |
This ensures that a vulnerability in one operation cannot compromise the other key.
Security Properties#
| Property | Implementation |
|---|---|
| Confidentiality | AES-256-CBC |
| Integrity | HMAC-SHA256 (Encrypt-then-MAC) |
| Key Separation | HKDF-SHA256 |
| Timing Safety | hash_equals() for MAC verification |
| IV Randomness | random_bytes() (CSPRNG) |
Best Practices#
- Always use `APP_KEY` >= 32 characters — shorter keys are rejected
- Keep `APP_KEY` secret — never commit to version control
- Rotate keys periodically — re-encrypt data with new key
- Use environment-specific keys — different keys for dev/staging/production
- Never encrypt authentication tokens — use JWT instead
Available Methods#
| Method | Description |
|---|---|
encrypt(string $data, ?string $key) | Encrypt data with optional custom key |
decrypt(string $payload, ?string $key) | Decrypt payload with optional custom key |
generateIv() | Generate random 16-byte IV |
hkdf(string $key, string $salt) | Derive sub-key using HKDF-SHA256 |