Skip to content

Commit

Permalink
extract key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 3, 2017
1 parent 02ccd2e commit 6623996
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Encryption/Encrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Foundation/Console/KeyGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Encryption\Encrypter;
use Illuminate\Console\ConfirmableTrait;

class KeyGenerateCommand extends Command
Expand Down Expand Up @@ -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'])
);
}

/**
Expand Down

0 comments on commit 6623996

Please sign in to comment.