Skip to content
Snippets Groups Projects
Select Git revision
  • a6ffb282610159d168a1e64cffdd5d1c0ab5d78f
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.4 protected
  • 11.2.5 protected
  • 10.5.3 protected
  • 11.2.4 protected
  • 10.5.2 protected
  • 11.2.3 protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
41 results

PrivateKey.php

  • webchick's avatar
    Issue #2140433 by pwolanin, chx, Owen Barton, dstol, Heine, Damien Tournoud:...
    Angie Byron authored
    Issue #2140433 by pwolanin, chx, Owen Barton, dstol, Heine, Damien Tournoud: Port SA-CORE-2013-003 to Drupal 8.
    b8ef3c6b
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    PrivateKey.php 1.16 KiB
    <?php
    
    /**
     * @file
     * Contains \Drupal\Core\PrivateKey.
     */
    
    namespace Drupal\Core;
    
    use Drupal\Core\KeyValueStore\StateInterface;
    use Drupal\Component\Utility\Crypt;
    
    /**
     * Manages the Drupal private key.
     */
    class PrivateKey {
    
      /**
       * The state service.
       *
       * @var \Drupal\Core\KeyValueStore\StateInterface
       */
      protected $state;
    
      /**
       * Constructs the token generator.
       *
       * @param \Drupal\Core\KeyValueStore\StateInterface $state
       *   The state service.
       */
      function __construct(StateInterface $state) {
        $this->state = $state;
      }
    
      /**
       * Gets the private key.
       *
       * @return string
       *   The private key.
       */
      public function get() {
        if (!$key = $this->state->get('system.private_key')) {
          $key = $this->create();
          $this->set($key);
        }
    
        return $key;
      }
    
      /**
       * Sets the private key.
       *
       * @param string $key
       *  The private key to set.
       */
      public function set($key) {
        return $this->state->set('system.private_key', $key);
      }
    
      /**
       * Creates a new private key.
       *
       * @return string
       *   The private key.
       */
      protected function create() {
        return Crypt::randomBytesBase64(55);
      }
    
    }