Skip to content

Commit 17ce4ed

Browse files
committedMar 25, 2016
Work on encryption service provider.
1 parent 2e0c5f4 commit 17ce4ed

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed
 

‎src/Illuminate/Encryption/EncryptionServiceProvider.php

+25-12
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\Support\ServiceProvider;
78

89
class EncryptionServiceProvider extends ServiceProvider
@@ -11,25 +12,37 @@ class EncryptionServiceProvider extends ServiceProvider
1112
* Register the service provider.
1213
*
1314
* @return void
14-
*
15-
* @throws \RuntimeException
1615
*/
1716
public function register()
1817
{
1918
$this->app->singleton('encrypter', function ($app) {
2019
$config = $app->make('config')->get('app');
2120

22-
$key = $config['key'];
23-
24-
$cipher = $config['cipher'];
25-
26-
if (Encrypter::supported($key, $cipher)) {
27-
return new Encrypter($key, $cipher);
28-
} elseif (McryptEncrypter::supported($key, $cipher)) {
29-
return new McryptEncrypter($key, $cipher);
30-
} else {
31-
throw new RuntimeException('No supported encrypter found. The cipher and / or key length are invalid.');
21+
if (Str::startsWith($key = $config['key'], 'base64:')) {
22+
$key = base64_decode(substr($key, 7));
3223
}
24+
25+
return $this->getEncrypterForKeyAndCipher($key, $config['cipher']);
3326
});
3427
}
28+
29+
/**
30+
* Get the proper encrypter instance for the given key and cipher.
31+
*
32+
* @param string $key
33+
* @param string $cipher
34+
* @return mixed
35+
*
36+
* @throws \RuntimeException
37+
*/
38+
protected function getEncrypterForKeyAndCipher($key, $cipher)
39+
{
40+
if (Encrypter::supported($key, $cipher)) {
41+
return new Encrypter($key, $cipher);
42+
} elseif (McryptEncrypter::supported($key, $cipher)) {
43+
return new McryptEncrypter($key, $cipher);
44+
} else {
45+
throw new RuntimeException('No supported encrypter found. The cipher and / or key length are invalid.');
46+
}
47+
}
3548
}

0 commit comments

Comments
 (0)
Please sign in to comment.