OpenSSLEncryption.php in Ubercart 8.4
File
uc_store/src/OpenSSLEncryption.php
View source
<?php
namespace Drupal\uc_store;
use Drupal\Component\Utility\Crypt;
class OpenSSLEncryption implements EncryptionInterface {
protected $cypher = 'AES-128-CBC';
protected $errors = [];
public function getErrors() {
$errors = $this->errors;
$this->errors = [];
return $errors;
}
public function setErrors(array $errors) {
$this->errors = $errors;
return $this;
}
public function setCypher($cypher) {
$methods = openssl_get_cipher_methods();
if (in_array($cypher, $methods)) {
$this->cypher = $cypher;
}
else {
$this->errors[] = t('@cypher is not a valid cypher', [
'@cypher' => $cypher,
]);
}
return $this;
}
public function encrypt($key, $plaintext, $sourcelen = 0) {
$iv = Crypt::randomBytes(16);
$encrypted = openssl_encrypt($plaintext, $this->cypher, $key, OPENSSL_RAW_DATA, $iv);
if (FALSE === $encrypted) {
$this->errors[] = t('Unknown error encrypting plaintext.');
}
return bin2hex($iv) . base64_encode($encrypted);
}
public function decrypt($key, $cyphertext) {
$iv = hex2bin(substr($cyphertext, 0, 32));
$decrypted = openssl_decrypt(base64_decode(substr($cyphertext, 32)), $this->cypher, $key, OPENSSL_RAW_DATA, $iv);
if (FALSE === $decrypted) {
$this->errors[] = t('Unknown error decrypting plaintext.');
}
return $decrypted;
}
}