From 6623996212b3d59aa31a374b70311f03fd158075 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 3 Jul 2017 15:46:16 -0500 Subject: [PATCH] extract key generation --- src/Illuminate/Encryption/Encrypter.php | 11 +++++++++++ .../Foundation/Console/KeyGenerateCommand.php | 7 ++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Encryption/Encrypter.php b/src/Illuminate/Encryption/Encrypter.php index 05c15e9a1966..21f256d654c8 100755 --- a/src/Illuminate/Encryption/Encrypter.php +++ b/src/Illuminate/Encryption/Encrypter.php @@ -59,6 +59,17 @@ public static function supported($key, $cipher) ($cipher === 'AES-256-CBC' && $length === 32); } + /** + * Create a new encryption key for the given cipher. + * + * @param string $cipher + * @return string + */ + public static function generateKey($cipher) + { + return random_bytes($cipher == 'AES-128-CBC' ? 16 : 32); + } + /** * Encrypt the given value. * diff --git a/src/Illuminate/Foundation/Console/KeyGenerateCommand.php b/src/Illuminate/Foundation/Console/KeyGenerateCommand.php index e280a06e81fb..52ac6ea0d176 100644 --- a/src/Illuminate/Foundation/Console/KeyGenerateCommand.php +++ b/src/Illuminate/Foundation/Console/KeyGenerateCommand.php @@ -3,6 +3,7 @@ namespace Illuminate\Foundation\Console; use Illuminate\Console\Command; +use Illuminate\Encryption\Encrypter; use Illuminate\Console\ConfirmableTrait; class KeyGenerateCommand extends Command @@ -57,9 +58,9 @@ public function handle() */ protected function generateRandomKey() { - return 'base64:'.base64_encode(random_bytes( - $this->laravel['config']['app.cipher'] == 'AES-128-CBC' ? 16 : 32 - )); + return 'base64:'.base64_encode( + Encrypter::generateKey($this->laravel['config']['app.cipher']) + ); } /**