ConfigTestEncryptionMethod.php in Encrypt 8.3
File
tests/modules/encrypt_test/src/Plugin/EncryptionMethod/ConfigTestEncryptionMethod.php
View source
<?php
namespace Drupal\encrypt_test\Plugin\EncryptionMethod;
use Drupal\Core\Form\FormStateInterface;
use Drupal\encrypt\EncryptionMethodInterface;
use Drupal\encrypt\Plugin\EncryptionMethod\EncryptionMethodBase;
use Drupal\encrypt\Plugin\EncryptionMethodPluginFormInterface;
class ConfigTestEncryptionMethod extends EncryptionMethodBase implements EncryptionMethodInterface, EncryptionMethodPluginFormInterface {
public function checkDependencies($text = NULL, $key = NULL) {
$errors = [];
return $errors;
}
public function encrypt($text, $key, $options = []) {
$prefix = $key . $this
->getConfiguration()['mode'];
return str_rot13($prefix . $text);
}
public function decrypt($text, $key, $options = []) {
$decoded = str_rot13($text);
$prefix = $key . $this
->getConfiguration()['mode'];
if (substr($decoded, 0, strlen($prefix)) == $prefix) {
return substr($decoded, strlen($prefix));
}
else {
return "invalid key";
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['mode'] = [
'#type' => 'select',
'#title' => $this
->t('Cipher mode'),
'#options' => [
'ECB' => $this
->t('ECB'),
'CBC' => $this
->t('CBC'),
'CFB' => $this
->t('CFB'),
'OFB' => $this
->t('OFB'),
],
'#default_value' => 'CBC',
'#required' => TRUE,
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration($form_state
->getValues());
}
}