/** * Update the EncryptionMethod plugin. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ protected function updateEncryptionMethod(FormStateInterface $form_state) { /* @var $encryption_profile \Drupal\encrypt\Entity\EncryptionProfile */ $encryption_profile = $this->entity; /* @var $plugin \Drupal\encrypt\EncryptionMethodInterface */ $plugin = $encryption_profile->getEncryptionMethod(); $encryption_profile->setEncryptionMethod($plugin); // If an original profile exists and the plugin ID matches the existing one. if ($this->originalProfile && $this->originalProfile->getEncryptionMethod()->getPluginId() == $plugin->getPluginId()) { // Use the configuration from the original profile's plugin. $configuration = $this->originalProfile->getEncryptionMethod()->getConfiguration(); } else { // Use the plugin's default configuration. $configuration = $plugin->defaultConfiguration(); } $plugin->setConfiguration($configuration); $form_state->setValue('encryption_method_configuration', []); $form_state->getUserInput()['encryption_method_configuration'] = []; }
/** * Get the used encryption key. * * @param string $text * The text to encrypt / decrypt. * @param \Drupal\encrypt\Entity\EncryptionProfile $encryption_profile * The encryption profile to use. * * @return string * The encryption key value. * * @throws \Drupal\encrypt\Exception\EncryptException * Can throw an EncryptException. */ protected function getEncryptionKeyValue($text, EncryptionProfile $encryption_profile) { // Load the encryption method. $enc_method = $encryption_profile->getEncryptionMethod(); $this->encryptionMethod = $this->encryptManager->createInstance($enc_method); // Load the encryption key. $key_value = $this->loadEncryptionProfileKey($encryption_profile); // Check for missing dependencies. $errors = $this->encryptionMethod->checkDependencies($text, $key_value); if (!empty($errors)) { // Throw an exception with the errors from the encryption method. throw new EncryptException(implode('; ', $errors)); } else { return $key_value; } }
/** * Loads an encryption profile method. * @param \Drupal\encrypt\Entity\EncryptionProfile $enc_profile */ private function loadEncryptionMethod($enc_profile) { // Load the encryption method. $enc_method = $enc_profile->getEncryptionMethod(); return $this->encryptManager->createInstance($enc_method); }