3
3
namespace Illuminate \Encryption ;
4
4
5
5
use RuntimeException ;
6
+ use Illuminate \Support \Str ;
6
7
use Illuminate \Support \ServiceProvider ;
7
8
8
9
class EncryptionServiceProvider extends ServiceProvider
@@ -11,25 +12,37 @@ class EncryptionServiceProvider extends ServiceProvider
11
12
* Register the service provider.
12
13
*
13
14
* @return void
14
- *
15
- * @throws \RuntimeException
16
15
*/
17
16
public function register ()
18
17
{
19
18
$ this ->app ->singleton ('encrypter ' , function ($ app ) {
20
19
$ config = $ app ->make ('config ' )->get ('app ' );
21
20
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 ));
32
23
}
24
+
25
+ return $ this ->getEncrypterForKeyAndCipher ($ key , $ config ['cipher ' ]);
33
26
});
34
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
+ }
35
48
}
0 commit comments