Installation:
composer require corley/rc4-bundle
Add to config/bundles.php:
return [
// ...
Corley\RC4Bundle\CorleyRC4Bundle::class => ['all' => true],
];
Configure Key:
Add your RC4 key in config/packages/rc4.yaml (or parameters.yml for older Laravel):
rc4:
key: "your-32-character-secret-key-here"
First Use Case: Inject the RC4 service into a controller or service:
use Corley\RC4Bundle\Service\RC4Service;
class MyController extends Controller
{
public function __construct(private RC4Service $rc4) {}
public function encrypt()
{
$encrypted = $this->rc4->encrypt("plaintext");
$decrypted = $this->rc4->decrypt($encrypted);
return response()->json(['encrypted' => $encrypted]);
}
}
Service Binding: Register the service in Laravel's container (if not auto-discovered):
$this->app->bind(RC4Service::class, function ($app) {
return new RC4Service($app['config']['rc4.key']);
});
Usage in Services:
class DataEncryptor
{
public function __construct(private RC4Service $rc4) {}
public function secureData(string $data): string
{
return $this->rc4->encrypt($data);
}
}
API Security:
$user->api_token = $this->rc4->encrypt($token);
$user->save();
$token = $this->rc4->decrypt($user->api_token);
Configuration Management:
config/rc4.php:
return [
'key' => env('RC4_KEY', 'fallback-key'),
];
Middleware for Encryption:
class EncryptPayloadMiddleware
{
public function handle($request, Closure $next)
{
$request->merge([
'payload' => $this->rc4->encrypt($request->payload)
]);
return $next($request);
}
}
Laravel Facades: Create a facade for cleaner syntax:
// app/Facades/RC4.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class RC4 extends Facade
{
protected static function getFacadeAccessor() { return 'rc4'; }
}
Usage:
$encrypted = RC4::encrypt("data");
Environment Variables:
Use Laravel's .env for the key:
RC4_KEY=your-32-character-secret-key-here
Then reference in config/rc4.php:
'key' => env('RC4_KEY'),
Key Length:
if (strlen($this->key) !== 32) {
throw new \RuntimeException("RC4 key must be 32 characters long.");
}
Deprecated Package:
openssl_encrypt or defuse/php-encryption.composer.json:
"corley/rc4-bundle": "dev-master"
No Padding/Error Handling:
$this->rc4->encrypt(str_pad($data, 16, "\0")); // Example padding
Symfony2 Legacy:
Test Key: Use a known key/value pair to verify:
$this->assertEquals("expected", $this->rc4->encrypt("test"));
Logging: Log encrypted/decrypted values for debugging:
\Log::debug("RC4 Encrypted", ['data' => $encrypted]);
Custom Service: Extend the service for additional methods:
class ExtendedRC4Service extends RC4Service
{
public function hash(string $data): string
{
return hash('sha256', $this->encrypt($data));
}
}
Configuration Overrides: Override the key dynamically:
$this->app->singleton(RC4Service::class, function ($app) {
return new RC4Service($app['config']['rc4.key'] ?? 'dynamic-key');
});
Event Listeners: Trigger events on encryption/decryption:
$this->rc4->encrypt($data); // Listen for `rc4.encrypted` event
How can I help you explore Laravel packages today?