Skip to content

Commit 370ae34

Browse files
committedMar 25, 2016
use full 256 bytes for selection of random keys... support base64 encoded keys.
1 parent 7f35988 commit 370ae34

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed
 

‎src/Illuminate/Encryption/Encrypter.php

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Encryption;
44

55
use RuntimeException;
6+
use Illuminate\Support\Str;
67
use Illuminate\Contracts\Encryption\DecryptException;
78
use Illuminate\Contracts\Encryption\EncryptException;
89
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
@@ -29,6 +30,10 @@ public function __construct($key, $cipher = 'AES-128-CBC')
2930
{
3031
$key = (string) $key;
3132

33+
if (Str::startsWith($key, 'base64:')) {
34+
$key = base64_decode(substr($key, 7));
35+
}
36+
3237
if (static::supported($key, $cipher)) {
3338
$this->key = $key;
3439
$this->cipher = $cipher;

‎src/Illuminate/Foundation/Application.php

+10
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,16 @@ public function environmentFile()
441441
return $this->environmentFile ?: '.env';
442442
}
443443

444+
/**
445+
* Get the fully qualified path to the environment file.
446+
*
447+
* @return string
448+
*/
449+
public function environmentFilePath()
450+
{
451+
return $this->environmentPath().'/'.$this->environmentFile();
452+
}
453+
444454
/**
445455
* Get or check the current application environment.
446456
*

‎src/Illuminate/Foundation/Console/KeyGenerateCommand.php

+23-33
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
use Illuminate\Support\Str;
66
use Illuminate\Console\Command;
7-
use Symfony\Component\Console\Input\InputOption;
87

98
class KeyGenerateCommand extends Command
109
{
1110
/**
12-
* The console command name.
11+
* The name and signature of the console command.
1312
*
1413
* @var string
1514
*/
16-
protected $name = 'key:generate';
15+
protected $signature = 'key:generate {--show : Display the key instead of modifying files}';
1716

1817
/**
1918
* The console command description.
@@ -29,55 +28,46 @@ class KeyGenerateCommand extends Command
2928
*/
3029
public function fire()
3130
{
32-
$app = $this->laravel;
33-
34-
$key = $this->getRandomKey($app['config']['app.cipher']);
31+
$key = $this->generateRandomKey();
3532

3633
if ($this->option('show')) {
3734
return $this->line('<comment>'.$key.'</comment>');
3835
}
3936

40-
$path = $app->environmentPath().'/'.$app->environmentFile();
41-
42-
if (file_exists($path)) {
43-
$content = str_replace('APP_KEY='.$app['config']['app.key'], 'APP_KEY='.$key, file_get_contents($path));
44-
45-
if (! Str::contains($content, 'APP_KEY')) {
46-
$content = sprintf("%s\nAPP_KEY=%s\n", $content, $key);
47-
}
37+
// Next, we will replace the application key in the environment file so it is
38+
// automatically setup for this developer. This key gets generated using a
39+
// secure random byte generator and is later base64 encoded for storage.
40+
$this->setKeyInEnvironmentFile($key);
4841

49-
file_put_contents($path, $content);
50-
}
51-
52-
$app['config']['app.key'] = $key;
42+
$this->laravel['config']['app.key'] = $key;
5343

5444
$this->info("Application key [$key] set successfully.");
5545
}
5646

5747
/**
58-
* Generate a random key for the application.
48+
* Set the environmeny key in the environment file.
5949
*
60-
* @param string $cipher
61-
* @return string
50+
* @param string $key
51+
* @return void
6252
*/
63-
protected function getRandomKey($cipher)
53+
protected function setKeyInEnvironmentFile($key)
6454
{
65-
if ($cipher === 'AES-128-CBC') {
66-
return Str::random(16);
67-
}
68-
69-
return Str::random(32);
55+
file_put_contents($this->laravel->environmentFilePath(), str_replace(
56+
'APP_KEY='.$this->laravel['config']['app.key'],
57+
'APP_KEY='.$key,
58+
file_get_contents($this->laravel->environmentFilePath())
59+
));
7060
}
7161

7262
/**
73-
* Get the console command options.
63+
* Generate a random key for the application.
7464
*
75-
* @return array
65+
* @return string
7666
*/
77-
protected function getOptions()
67+
protected function generateRandomKey()
7868
{
79-
return [
80-
['show', null, InputOption::VALUE_NONE, 'Simply display the key instead of modifying files.'],
81-
];
69+
return 'base64:'.base64_encode(random_bytes(
70+
$this->laravel['config']['app.cipher'] == 'AES-128-CBC' ? 16 : 32
71+
));
8272
}
8373
}

‎tests/Encryption/EncrypterTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,25 @@ public function testEncryption()
1313
$this->assertEquals('foo', $e->decrypt($encrypted));
1414
}
1515

16+
public function testEncryptionUsingBase64EncodedKey()
17+
{
18+
$e = new Encrypter('base64:'.base64_encode(random_bytes(16)));
19+
$encrypted = $e->encrypt('foo');
20+
$this->assertNotEquals('foo', $encrypted);
21+
$this->assertEquals('foo', $e->decrypt($encrypted));
22+
}
23+
1624
public function testWithCustomCipher()
1725
{
1826
$e = new Encrypter(str_repeat('b', 32), 'AES-256-CBC');
1927
$encrypted = $e->encrypt('bar');
2028
$this->assertNotEquals('bar', $encrypted);
2129
$this->assertEquals('bar', $e->decrypt($encrypted));
30+
31+
$e = new Encrypter('base64:'.base64_encode(random_bytes(32)), 'AES-256-CBC');
32+
$encrypted = $e->encrypt('foo');
33+
$this->assertNotEquals('foo', $encrypted);
34+
$this->assertEquals('foo', $e->decrypt($encrypted));
2235
}
2336

2437
/**

0 commit comments

Comments
 (0)
Please sign in to comment.