This commit is contained in:
Yuri Kuznetsov
2022-03-09 12:06:26 +02:00
parent 5c811b1861
commit 196488f92d
5 changed files with 20 additions and 58 deletions
@@ -30,17 +30,16 @@
namespace Espo\Core\Authentication\AuthToken;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\RDBRepository;
use Espo\Entities\AuthToken as AuthTokenEntity;
use RuntimeException;
use const MCRYPT_DEV_URANDOM;
class EspoManager implements Manager
{
private $entityManager;
private EntityManager $entityManager;
private $repository;
private RDBRepository $repository;
private const TOKEN_RANDOM_LENGTH = 16;
@@ -163,10 +162,6 @@ class EspoManager implements Manager
return bin2hex(random_bytes($length));
}
if (function_exists('mcrypt_create_iv')) {
return bin2hex(mcrypt_create_iv($length, MCRYPT_DEV_URANDOM));
}
if (function_exists('openssl_random_pseudo_bytes')) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
@@ -51,6 +51,7 @@ class Manager
/**
* @var array<string,resource>
* @phpstan-ignore-next-line Used to prevent deleting from memory.
*/
private $resourceMap = [];
+16 -33
View File
@@ -33,11 +33,11 @@ use RuntimeException;
class Crypt
{
private $key = null;
private string $cryptKey;
private $cryptKey = null;
private ?string $key = null;
private $iv = null;
private ?string $iv = null;
public function __construct(Config $config)
{
@@ -51,7 +51,7 @@ class Crypt
}
if (!$this->key) {
throw new RuntimeException("Could not hash key.");
throw new RuntimeException("Could not hash the key.");
}
return $this->key;
@@ -60,12 +60,11 @@ class Crypt
private function getIv(): string
{
if ($this->iv === null) {
if (extension_loaded('openssl')) {
$this->iv = openssl_random_pseudo_bytes(16);
}
else {
$this->iv = mcrypt_create_iv(16, MCRYPT_RAND);
if (!extension_loaded('openssl')) {
throw new RuntimeException("openssl extension is not loaded.");
}
$this->iv = openssl_random_pseudo_bytes(16);
}
return $this->iv;
@@ -75,44 +74,28 @@ class Crypt
{
$iv = $this->getIv();
if (extension_loaded('openssl')) {
return base64_encode(
openssl_encrypt($string, 'aes-256-cbc', $this->getKey(), OPENSSL_RAW_DATA , $iv) . $iv
);
if (!extension_loaded('openssl')) {
throw new RuntimeException("openssl extension is not loaded.");
}
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'cbc');
$pad = $block - (strlen($string) % $block);
$string .= str_repeat(chr($pad), $pad);
return base64_encode(
mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->getKey(), $string, MCRYPT_MODE_CBC, $iv) . $iv
openssl_encrypt($string, 'aes-256-cbc', $this->getKey(), OPENSSL_RAW_DATA, $iv) . $iv
);
}
public function decrypt(string $encryptedString): string
{
$encryptedStringDecoded = base64_decode($encryptedString);
$string = substr($encryptedStringDecoded, 0, strlen($encryptedStringDecoded) - 16);
$iv = substr($encryptedStringDecoded, -16);
if (extension_loaded('openssl')) {
return trim(
openssl_decrypt($string, 'aes-256-cbc', $this->getKey(), OPENSSL_RAW_DATA, $iv)
);
if (!extension_loaded('openssl')) {
throw new RuntimeException("openssl extension is not loaded.");
}
$string = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->getKey(), $string, MCRYPT_MODE_CBC, $iv);
$len = strlen($string);
$pad = ord($string[$len - 1]);
return substr($string, 0, strlen($string) - $pad);
return trim(
openssl_decrypt($string, 'aes-256-cbc', $this->getKey(), OPENSSL_RAW_DATA, $iv)
);
}
public function generateKey(): string
-7
View File
@@ -62,11 +62,6 @@ class SthCollection implements Collection, IteratorAggregate, Countable
private ?string $sql = null;
/**
* @var Entity[]
*/
private array $entityList = [];
private function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
@@ -126,8 +121,6 @@ class SthCollection implements Collection, IteratorAggregate, Countable
$this->prepareEntity($entity);
$this->entityList[] = $entity;
yield $entity;
}
})();
-10
View File
@@ -37,8 +37,6 @@ use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\{
Acl,
AclManager,
Select\SelectBuilderFactory,
DataManager,
InjectableFactory,
Utils\Metadata,
@@ -72,12 +70,8 @@ class App
private $acl;
private $aclManager;
private $dataManager;
private $selectBuilderFactory;
private $injectableFactory;
private $settingsService;
@@ -95,9 +89,7 @@ class App
EntityManager $entityManager,
Metadata $metadata,
Acl $acl,
AclManager $aclManager,
DataManager $dataManager,
SelectBuilderFactory $selectBuilderFactory,
InjectableFactory $injectableFactory,
SettingsService $settingsService,
User $user,
@@ -109,9 +101,7 @@ class App
$this->entityManager = $entityManager;
$this->metadata = $metadata;
$this->acl = $acl;
$this->aclManager = $aclManager;
$this->dataManager = $dataManager;
$this->selectBuilderFactory = $selectBuilderFactory;
$this->injectableFactory = $injectableFactory;
$this->settingsService = $settingsService;
$this->user = $user;