EncryptionServiceProvider.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Illuminate\Encryption;
  3. use RuntimeException;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\ServiceProvider;
  6. class EncryptionServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * Register the service provider.
  10. *
  11. * @return void
  12. */
  13. public function register()
  14. {
  15. $this->app->singleton('encrypter', function ($app) {
  16. $config = $app->make('config')->get('app');
  17. // If the key starts with "base64:", we will need to decode the key before handing
  18. // it off to the encrypter. Keys may be base-64 encoded for presentation and we
  19. // want to make sure to convert them back to the raw bytes before encrypting.
  20. if (Str::startsWith($key = $this->key($config), 'base64:')) {
  21. $key = base64_decode(substr($key, 7));
  22. }
  23. return new Encrypter($key, $config['cipher']);
  24. });
  25. }
  26. /**
  27. * Extract the encryption key from the given configuration.
  28. *
  29. * @param array $config
  30. * @return string
  31. */
  32. protected function key(array $config)
  33. {
  34. return tap($config['key'], function ($key) {
  35. if (empty($key)) {
  36. throw new RuntimeException(
  37. 'No application encryption key has been specified.'
  38. );
  39. }
  40. });
  41. }
  42. }