1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Encryption;
|
4
|
|
5
|
use RuntimeException;
|
6
|
use Illuminate\Support\Str;
|
7
|
use Illuminate\Support\ServiceProvider;
|
8
|
|
9
|
class EncryptionServiceProvider extends ServiceProvider
|
10
|
{
|
11
|
/**
|
12
|
* Register the service provider.
|
13
|
*
|
14
|
* @return void
|
15
|
*/
|
16
|
public function register()
|
17
|
{
|
18
|
$this->app->singleton('encrypter', function ($app) {
|
19
|
$config = $app->make('config')->get('app');
|
20
|
|
21
|
if (Str::startsWith($key = $config['key'], 'base64:')) {
|
22
|
$key = base64_decode(substr($key, 7));
|
23
|
}
|
24
|
|
25
|
return $this->getEncrypterForKeyAndCipher($key, $config['cipher']);
|
26
|
});
|
27
|
}
|
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
|
}
|
48
|
}
|