/** * {@inheritdoc} */ function decrypt($text) { // Get settings. $settings = $this->config->get('encrypt.settings'); // Load the key. $key = $this->key->getKey($settings->get('encryption_key')); // Load the encryption method. $enc_method = $this->manager->createInstance($settings->get('encryption_method')); // Return the encrypted string. return $enc_method->decrypt($text, $key->getKeyValue()); }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('encrypt.settings'); $keys = []; foreach ($this->key_manager->getKeys() as $key) { $key_id = $key->id(); $key_title = $key->label(); $keys[$key_id] = (string) $key_title; } $form['encryption_key'] = array('#type' => 'select', '#title' => $this->t('Encryption Key'), '#description' => $this->t('Select the key used for encryption'), '#options' => $keys, '#default_value' => $config->get('encryption_key')); $enc_methods = []; foreach ($this->encrypt_service->loadEncryptionMethods() as $plugin_id => $definition) { $enc_methods[$plugin_id] = (string) $definition['title']; } $form['encryption_method'] = array('#type' => 'select', '#title' => $this->t('Encryption Method'), '#description' => $this->t('Select the method used for encryption'), '#options' => $enc_methods, '#default_value' => $config->get('encryption_method')); return parent::buildForm($form, $form_state); }
/** * Test get keys by storage method. * * @group key */ public function testGetKeysByStorageMethod() { // Create a key provider plugin to play with. $defaults = ['key_value' => $this->createToken()]; $definition = ['id' => 'config', 'class' => 'Drupal\\key\\Plugin\\KeyProvider\\ConfigKeyProvider', 'title' => 'Configuration']; $KeyProvider = new ConfigKeyProvider($defaults, 'config', $definition); // Mock the loadByProperties method in entity manager. $this->configStorage->expects($this->any())->method('loadByProperties')->with(['key_provider' => 'config'])->willReturn([$this->key_id => $this->key]); // Make the key provider plugin manager return a plugin instance. $this->KeyProviderManager->expects($this->any())->method('createInstance')->with('config', $defaults)->willReturn($KeyProvider); $this->key->set('key_settings', $defaults); $keys = $this->keyManager->getKeysByStorageMethod('config'); $this->assertEquals([$this->key_id => $this->key], $keys); }