RealAESEncryptionMethod.php in Real AES 8.2
File
src/Plugin/EncryptionMethod/RealAESEncryptionMethod.php
View source
<?php
namespace Drupal\real_aes\Plugin\EncryptionMethod;
use Drupal\encrypt\EncryptionMethodInterface;
use Drupal\encrypt\Plugin\EncryptionMethod\EncryptionMethodBase;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Exception as Ex;
use Defuse\Crypto\Key;
use Defuse\Crypto\Encoding;
class RealAESEncryptionMethod extends EncryptionMethodBase implements EncryptionMethodInterface {
public function checkDependencies($text = NULL, $key = NULL) {
$errors = [];
if (!class_exists('\\Defuse\\Crypto\\Crypto')) {
$errors[] = $this
->t('Defuse PHP Encryption library is not correctly installed.');
}
if (strlen($key) != Key::KEY_BYTE_SIZE) {
$errors[] = $this
->t("This encryption method requires a @size byte key.", [
'@size' => Key::KEY_BYTE_SIZE,
]);
}
return $errors;
}
public function encrypt($text, $key, $options = []) {
try {
$key = Encoding::saveBytesToChecksummedAsciiSafeString(Key::KEY_CURRENT_VERSION, $key);
$key = Key::loadFromAsciiSafeString($key);
return Crypto::encrypt((string) $text, $key);
} catch (Ex\CryptoException $ex) {
return FALSE;
}
}
public function decrypt($text, $key, $options = []) {
try {
$key = Encoding::saveBytesToChecksummedAsciiSafeString(Key::KEY_CURRENT_VERSION, $key);
$key = Key::loadFromAsciiSafeString($key);
return Crypto::decrypt((string) $text, $key);
} catch (Ex\CryptoException $ex) {
return FALSE;
}
}
}