class McryptAES128Encryption in Two-factor Authentication (TFA) 8
Deprecated Mcrypt AES 128 encryption plugin.
@package Drupal\encrypt\Plugin\EncryptionMethod
@EncryptionMethod( id = "mcrypt_aes_128", title = @Translation("Mcrypt AES 128"), description = "This uses PHP OpenSSL or Mcrypt extensions and <a href='http://en.wikipedia.org/wiki/Advanced_Encryption_Standard'>AES-128</a>.", key_type = {"encryption"}, can_decrypt = TRUE, deprecated = TRUE )
phpcs:disable PHPCompatibility
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\encrypt\Plugin\EncryptionMethod\EncryptionMethodBase implements ConfigurableInterface, DependentPluginInterface, EncryptionMethodInterface
- class \Drupal\tfa\Plugin\EncryptionMethod\McryptAES128Encryption implements EncryptionMethodInterface uses StringTranslationTrait
- class \Drupal\encrypt\Plugin\EncryptionMethod\EncryptionMethodBase implements ConfigurableInterface, DependentPluginInterface, EncryptionMethodInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of McryptAES128Encryption
File
- src/
Plugin/ EncryptionMethod/ McryptAES128Encryption.php, line 26
Namespace
Drupal\tfa\Plugin\EncryptionMethodView source
class McryptAES128Encryption extends EncryptionMethodBase implements EncryptionMethodInterface {
use StringTranslationTrait;
const CRYPT_VERSION = 1;
/**
* {@inheritdoc}
*/
public function encrypt($text, $key) {
// Backwards compatibility with Mcrypt.
if (!extension_loaded('openssl') && extension_loaded('mcrypt')) {
return $this
->encryptWithMcrypt($text, $key);
}
// Encrypt using OpenSSL.
$iv = random_bytes(16);
$ciphertext = openssl_encrypt($text, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
$crypto_data = [
'version' => self::CRYPT_VERSION,
'iv_base64' => base64_encode($iv),
'ciphertext_base64' => base64_encode($ciphertext),
];
return Json::encode($crypto_data);
}
/**
* Encrypt using the deprecated Mcrypt extension.
*
* @param string $text
* The text to be encrypted.
* @param string $key
* The key to encrypt the text with.
*
* @return string
* The encrypted text.
*
* @noinspection PhpDeprecationInspection
*/
private function encryptWithMcrypt($text, $key) {
// Key cannot be too long for this encryption.
$key = mb_substr($key, 0, 32);
// Define iv cipher.
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$processed_text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, $iv);
$processed_text = base64_encode($processed_text);
return $processed_text;
}
/**
* {@inheritdoc}
*/
public function decrypt($text, $key) {
$crypto_data = Json::decode($text);
if (empty($crypto_data['version']) || empty($crypto_data['iv_base64']) || empty($crypto_data['ciphertext_base64'])) {
// Backwards compatibility with the old Mcrypt scheme.
return extension_loaded('mcrypt') ? $this
->decryptLegacyDataWithMcrypt($text, $key) : $this
->decryptLegacyDataWithOpenSsl($text, $key);
}
else {
$iv = base64_decode($crypto_data['iv_base64']);
$ciphertext = base64_decode($crypto_data['ciphertext_base64']);
return openssl_decrypt($ciphertext, 'aes-256-cbc', $key, TRUE, $iv);
}
}
/**
* Use OpenSSL to decrypt data that was originally encrypted with Mcrypt.
*
* @param string $text
* The text to be decrypted.
* @param string $key
* The key to decrypt the text with.
*
* @return string|bool
* The decrypted text, or FALSE on failure.
*/
private function decryptLegacyDataWithOpenSsl($text, $key) {
$key = mb_substr($key, 0, 32);
$text = base64_decode($text);
return openssl_decrypt($text, 'aes-128-cbc', $key, OPENSSL_NO_PADDING);
}
/**
* Decrypt using the deprecated Mcrypt extension.
*
* @param string $text
* The text to be decrypted.
* @param string $key
* The key to decrypt the text with.
*
* @return string
* The decrypted text
*
* @noinspection PhpDeprecationInspection
*/
private function decryptLegacyDataWithMcrypt($text, $key) {
// Key cannot be too long for this encryption.
$key = mb_substr($key, 0, 32);
// Define iv cipher.
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$text = base64_decode($text);
// Decrypt text.
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, $iv));
}
/**
* Check dependencies for the encryption method.
*
* @param string $text
* The text to be checked.
* @param string $key
* The key to be checked.
*
* @return array
* An array of error messages, providing info on missing dependencies.
*/
public function checkDependencies($text = NULL, $key = NULL) {
$errors = [];
if (!extension_loaded('openssl') && !extension_loaded('mcrypt')) {
$errors[] = $this
->t('OpenSSL and Mcrypt extensions are not installed.');
}
// Check if we have a 128 bit key.
if (strlen($key) != 16) {
$errors[] = $this
->t('This encryption method requires a 128 bit key.');
}
return $errors;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EncryptionMethodBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
EncryptionMethodBase:: |
public | function |
Define if encryption method can also decrypt. Overrides EncryptionMethodInterface:: |
|
EncryptionMethodBase:: |
public static | function | ||
EncryptionMethodBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
EncryptionMethodBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
EncryptionMethodBase:: |
public | function |
Get the label. Overrides EncryptionMethodInterface:: |
|
EncryptionMethodBase:: |
public | function |
Define if encryption method is deprecated. Overrides EncryptionMethodInterface:: |
|
EncryptionMethodBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
EncryptionMethodBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
McryptAES128Encryption:: |
public | function |
Check dependencies for the encryption method. Overrides EncryptionMethodInterface:: |
|
McryptAES128Encryption:: |
constant | |||
McryptAES128Encryption:: |
public | function |
Decrypt text. Overrides EncryptionMethodInterface:: |
|
McryptAES128Encryption:: |
private | function | Decrypt using the deprecated Mcrypt extension. | |
McryptAES128Encryption:: |
private | function | Use OpenSSL to decrypt data that was originally encrypted with Mcrypt. | |
McryptAES128Encryption:: |
public | function |
Encrypt text. Overrides EncryptionMethodInterface:: |
|
McryptAES128Encryption:: |
private | function | Encrypt using the deprecated Mcrypt extension. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |