class OpenSSL in DRD Agent 8.3
Same name and namespace in other branches
- 4.0.x src/Crypt/Method/OpenSSL.php \Drupal\drd_agent\Crypt\Method\OpenSSL
Provides OpenSSL encryption functionality.
Hierarchy
- class \Drupal\drd_agent\Crypt\BaseMethod implements BaseMethodInterface
- class \Drupal\drd_agent\Crypt\Method\OpenSSL
Expanded class hierarchy of OpenSSL
2 string references to 'OpenSSL'
- Base::getMethods in src/
Crypt/ Base.php - Get a list of crypt methods, either just their ids or instances of each.
- OpenSSL::getLabel in src/
Crypt/ Method/ OpenSSL.php - Get the crypt method label.
File
- src/
Crypt/ Method/ OpenSSL.php, line 14
Namespace
Drupal\drd_agent\Crypt\MethodView source
class OpenSSL extends BaseMethod {
private $cipher;
private $iv;
private $password;
private $supportedCipher = [
'aes-256-ctr' => 32,
'aes-128-cbc' => 16,
];
/**
* {@inheritdoc}
*/
public function __construct(ContainerInterface $container, array $settings = []) {
parent::__construct($container);
$this->cipher = $settings['cipher'] ?? '';
$this->password = $settings['password'] ?? '';
}
/**
* {@inheritdoc}
*/
public function getLabel() : string {
return 'OpenSSL';
}
/**
* {@inheritdoc}
*/
public function getCipher() : string {
return $this->cipher;
}
/**
* {@inheritdoc}
*/
public function getPassword() : string {
return base64_decode($this->password);
}
/**
* {@inheritdoc}
*/
public function isAvailable() : bool {
return function_exists('openssl_encrypt');
}
/**
* {@inheritdoc}
*/
public function getCipherMethods() : array {
$result = [];
$available = openssl_get_cipher_methods();
foreach ($this->supportedCipher as $cipher => $keyLength) {
if (in_array($cipher, $available, TRUE)) {
$result[$cipher] = $cipher;
}
}
return $result;
}
/**
* {@inheritdoc}
*/
public function getIv() : string {
if (empty($this->iv)) {
$nonceSize = openssl_cipher_iv_length($this->cipher);
$strong = TRUE;
/** @noinspection CryptographicallySecureRandomnessInspection */
$this->iv = openssl_random_pseudo_bytes($nonceSize, $strong);
if ($strong === FALSE || $this->iv === FALSE) {
$this->logger
->warning('Your systm does not produce secure randomness.');
}
}
return $this->iv;
}
/**
* {@inheritdoc}
*/
public function encrypt(array $args) : string {
return empty($this->password) ? '' : openssl_encrypt(serialize($args), $this->cipher, $this
->getPassword(), OPENSSL_RAW_DATA, $this
->getIv());
}
/**
* {@inheritdoc}
*/
public function decrypt($body, $iv) {
$this->iv = $iv;
/** @noinspection UnserializeExploitsInspection */
return unserialize(openssl_decrypt($body, $this->cipher, $this
->getPassword(), OPENSSL_RAW_DATA, $this->iv));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BaseMethod:: |
protected | property | ||
BaseMethod:: |
protected | property | ||
BaseMethod:: |
private | function | Callback to encrypt and decrypt files. | |
BaseMethod:: |
public | function |
Encrypt a file. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
private | property | ||
OpenSSL:: |
private | property | ||
OpenSSL:: |
private | property | ||
OpenSSL:: |
private | property | ||
OpenSSL:: |
public | function |
Decode, decrypt and unserialize arguments from the other end. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
Encrypt and encode any list of arguments. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
Get the selected cipher. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
Get a list of available cipher methods. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
Get an initialiation vector. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
Get the crypt method label. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
Get the password. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
Find out if the crypt method is available. Overrides BaseMethodInterface:: |
|
OpenSSL:: |
public | function |
BaseMethod constructor. Overrides BaseMethod:: |