Skip to content

Instantly share code, notes, and snippets.

@valorin

valorin/.env Secret

Last active January 14, 2024 16:38
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save valorin/ce58cf55dedaf759b3aa7fcfb2fcf613 to your computer and use it in GitHub Desktop.
Save valorin/ce58cf55dedaf759b3aa7fcfb2fcf613 to your computer and use it in GitHub Desktop.
Laravel Security in Depth [Tip#1] - Security Tip: Custom Encryption Key
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:JZe9TqpS9jr9m8/0wN1I5SNpfw1uZsznq+eHoHdcRVQ=
APP_DEBUG=true
APP_URL=http://lsid.test
LOG_CHANNEL=stack
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lsid
DB_USERNAME=root
DB_PASSWORD=
DB_ENCRYPTION_KEY=base64:JwjensMs4du0OBRDJIYaUThhCqmOt1ZkjXjZSsij3Gg=
# ...
<?php
namespace App\Providers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
Model::encryptUsing(new Encrypter($this->databaseEncryptionKey(), config('app.cipher')));
}
protected function databaseEncryptionKey(): ?string
{
return base64_decode(Str::after(config('database.encryption_key'), 'base64:'));
}
}
<?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
* Encryption key to use for casting encrypted model attributes
* Falls back to app key if not set, so the app doesn't break on install
*/
'encryption_key' => env('DB_ENCRYPTION_KEY', env('APP_KEY')),
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment