Installation:
composer require dterranova/crypto-bundle:dev-master
AppKernel.php:
new dterranova\Bundle\CryptoBundle\dterranovaCryptoBundle(),
config.yml:
dterranova_crypto:
temp_folder: "%kernel.root_dir%/../web/temp_crypto"
chunk_file_size: 2 # in Mb
First Use Case: Encrypt a file in a controller or service:
use Symfony\Component\HttpFoundation\Response;
public function encryptAction($filePath, $key)
{
$crypto = $this->get('dterranova_crypto.crypto_adapter');
$encryptedFolder = $crypto->encryptFile($filePath, $key);
return new Response("File encrypted in: $encryptedFolder");
}
Chunked Encryption/Decryption:
// Encrypt a 100MB file without loading it entirely into memory
$crypto->encryptFile('/path/to/large_file.pdf', 'secure_key_123');
Service Integration:
dterranova_crypto.crypto_adapter into services for reusable encryption logic.class FileProcessor
{
protected $crypto;
public function __construct($crypto)
{
$this->crypto = $crypto;
}
public function processFile($filePath, $key)
{
$encryptedPath = $this->crypto->encryptFile($filePath, $key);
// Process encrypted file (e.g., upload to cloud)
}
}
Decryption Workflow:
$originalFilePath = '/path/to/original_file.pdf';
$crypto->decryptFile($originalFilePath, 'secure_key_123');
File Storage:
storage/encrypted/).Filesystem component to manage paths dynamically:
$tempFolder = $this->getParameter('dterranova_crypto.temp_folder');
Key Management:
ParameterBag).Event-Driven Processing:
$dispatcher->dispatch(new FileEncryptedEvent($encryptedFolder));
Temp Folder Permissions:
temp_folder is writable by the web server user.storage/logs/ for Permission denied errors.Chunk Size Trade-offs:
chunk_file_size: 2 is reasonable for most use cases.Filename Collisions:
$uniqueKey = uniqid();
$crypto->encryptFile("/path/to/file_$uniqueKey.pdf", $key);
Key Rotation:
Verify Encryption:
file_exists() to check if chunk folders are created:
$encryptedFolder = $crypto->encryptFile($filePath, $key);
var_dump(file_exists($encryptedFolder)); // Should return true
Memory Usage:
memory_get_usage() during encryption of large files.chunk_file_size if memory limits are hit.Custom Chunk Handlers:
CryptoAdapter class.class CustomCryptoAdapter extends \dterranova\Bundle\CryptoBundle\Service\CryptoAdapter
{
protected function processChunk($chunk, $key) {
// Custom logic (e.g., compression before encryption)
return parent::processChunk($chunk, $key);
}
}
Post-Processing Hooks:
KernelEvents::TERMINATE to clean up temp folders after decryption:
$event->getKernel()->getContainer()->get('dterranova_crypto.crypto_adapter')->cleanupTempFiles();
Logging:
config.yml:
monolog:
handlers:
main:
level: debug
Path Formatting:
temp_folder uses absolute paths (relative paths may fail in production).temp_folder: "%kernel.project_dir%/var/encrypted_chunks"
Environment-Specific Settings:
chunk_file_size per environment (e.g., smaller chunks in CI):
# config_dev.yml
dterranova_crypto:
chunk_file_size: 0.5 # For testing
How can I help you explore Laravel packages today?