update 2023-10-24 10:36:32

This commit is contained in:
Robot 2023-10-24 10:36:46 +02:00
parent b4ce04c07e
commit 99b12ca8c0
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
24 changed files with 295 additions and 438 deletions

View File

@ -13,6 +13,7 @@
class Ftp #Gold {
# ?FtpClient $client
# ?object $details
+ iables $signature
+ set(object $details) : Ftp
+ move(string $localPath, string $fileName) : bool
- connected() : bool

View File

@ -114,12 +114,13 @@ class Ftp implements Serverinterface
// make sure we have a string and it is not default or empty
if (StringHelper::check($this->details->signature))
{
// turn into variables
parse_str((string) $this->details->signature);
// turn into array of variables
$signature = [];
parse_str((string) $this->details->signature, $signature);
// set options
if (isset($options) && ArrayHelper::check($options))
if (isset($signature['options']) && ArrayHelper::check($signature['options']))
{
foreach ($options as $o__p0t1on => $vAln3)
foreach ($signature['options'] as $o__p0t1on => $vAln3)
{
if ('timeout' === $o__p0t1on)
{
@ -136,10 +137,10 @@ class Ftp implements Serverinterface
$options = [];
}
// get ftp object
if (isset($host) && $host != 'HOSTNAME' &&
isset($port) && $port != 'PORT_INT' &&
isset($username) && $username != 'user@name.com' &&
isset($password) && $password != 'password')
if (isset($signature['host']) && $signature['host'] != 'HOSTNAME' &&
isset($signature['port']) && $signature['port'] != 'PORT_INT' &&
isset($signature['username']) && $signature['username'] != 'user@name.com' &&
isset($signature['password']) && $signature['password'] != 'password')
{
// this is a singleton
return FtpClient::getInstance($host, $port, $options, $username, $password);

View File

@ -86,12 +86,13 @@
// make sure we have a string and it is not default or empty
if (StringHelper::check($this->details->signature))
{
// turn into variables
parse_str((string) $this->details->signature);
// turn into array of variables
$signature = [];
parse_str((string) $this->details->signature, $signature);
// set options
if (isset($options) && ArrayHelper::check($options))
if (isset($signature['options']) && ArrayHelper::check($signature['options']))
{
foreach ($options as $o__p0t1on => $vAln3)
foreach ($signature['options'] as $o__p0t1on => $vAln3)
{
if ('timeout' === $o__p0t1on)
{
@ -108,10 +109,10 @@
$options = [];
}
// get ftp object
if (isset($host) && $host != 'HOSTNAME' &&
isset($port) && $port != 'PORT_INT' &&
isset($username) && $username != 'user@name.com' &&
isset($password) && $password != 'password')
if (isset($signature['host']) && $signature['host'] != 'HOSTNAME' &&
isset($signature['port']) && $signature['port'] != 'PORT_INT' &&
isset($signature['username']) && $signature['username'] != 'user@name.com' &&
isset($signature['password']) && $signature['password'] != 'password')
{
// this is a singleton
return FtpClient::getInstance($host, $port, $options, $username, $password);

View File

@ -15,7 +15,7 @@ class Legacy #Gold {
# int $size
+ __construct(BASEAES $aes)
+ encrypt(string $string, string $key) : string
+ decrypt(string $string, string $key) : string
+ decrypt(string $string, string $key) : ?string
}
note right of Legacy::__construct
@ -35,7 +35,7 @@ note right of Legacy::decrypt
Decrypt a string as needed
since: 3.2.0
return: string
return: ?string
end note
@enduml

View File

@ -90,10 +90,10 @@ class Legacy implements Cryptinterface
* @param string $string The string to decrypt
* @param string $key The decryption key
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
public function decrypt(string $string, string $key): ?string
{
// remove base 64 encoding
$string = base64_decode($string);
@ -110,8 +110,11 @@ class Legacy implements Cryptinterface
// set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
return $this->aes->decrypt($string);
}
try {
return $this->aes->decrypt($string);
} catch (\Exception $ex) {
return null;
}
}
}

View File

@ -65,10 +65,10 @@
* @param string $string The string to decrypt
* @param string $key The decryption key
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
public function decrypt(string $string, string $key): ?string
{
// remove base 64 encoding
$string = base64_decode($string);
@ -85,5 +85,9 @@
// set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
return $this->aes->decrypt($string);
}
try {
return $this->aes->decrypt($string);
} catch (\Exception $ex) {
return null;
}
}

View File

@ -14,7 +14,8 @@ class Sftp #Gold {
# KeyLoader $key
# ?SftpClient $client
# ?object $details
+ __construct(KeyLoader $key)
# CMSApplication $app
+ __construct(KeyLoader $key, ?CMSApplication $app = null)
+ set(object $details) : Sftp
+ move(string $localPath, string $fileName) : bool
- connected() : bool

View File

@ -12,6 +12,9 @@
namespace VDM\Joomla\Componentbuilder\Server;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Application\CMSApplication;
use phpseclib3\Net\SFTP as SftpClient;
use VDM\Joomla\Componentbuilder\Crypt\KeyLoader;
use VDM\Joomla\Utilities\StringHelper;
@ -51,16 +54,26 @@ class Sftp implements Serverinterface
**/
protected ?object $details = null;
/**
* Application object.
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/**
* Constructor
*
* @param KeyLoader $key The key loader object.
* @param CMSApplication|null $app The app object.
*
* @since 3.2.0
*/
public function __construct(KeyLoader $key)
public function __construct(KeyLoader $key, ?CMSApplication $app = null)
{
$this->key = $key;
$this->app = $app ?: Factory::getApplication();
}
/**
@ -106,10 +119,21 @@ class Sftp implements Serverinterface
StringHelper::check($this->details->path) &&
$this->details->path !== '/')
{
$path = '/' . trim((string) $this->details->path, '/');
$path = trim((string) $this->details->path);
$path = '/' . trim($path, '/') . '/';
}
return $this->client->put($path . '/' . $fileName, $data);
try
{
return $this->client->put($path . trim($fileName), $data);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::sprintf('COM_COMPONENTBUILDER_MOVING_OF_THE_S_FAILED', $fileName) . ': ' . $e->getMessage(),
'Error'
);
}
}
return false;
@ -147,24 +171,23 @@ class Sftp implements Serverinterface
isset($this->details->username) && StringHelper::check($this->details->username))
{
// insure the port is set
$port = (isset($this->details->port) && is_numeric($this->details->port) && $this->details->port > 0)
? (int) $this->details->port : 22;
$port = (int)($this->details->port ?? 22);
// open the connection
$sftp = new SftpClient($this->details->host, $port);
// set the passphrase if it exist
$passphrase = $this->details->secret ?? null;
$passphrase = (isset($this->details->secret) && StringHelper::check(trim($this->details->secret))) ? trim($this->details->secret) : false;
// set the password if it exist
$password = $this->details->password ?? null;
$password = (isset($this->details->password) && StringHelper::check(trim($this->details->password))) ? trim($this->details->password) : false;
// now login based on authentication type
$key = null;
switch($this->details->authentication)
{
case 1: // password
$key = $this->details->password ?? null;
$key = $password ?? null;
$password = null;
break;
case 2: // private key file
@ -172,28 +195,77 @@ class Sftp implements Serverinterface
if (isset($this->details->private) && StringHelper::check($this->details->private) &&
($private_key = FileHelper::getContent($this->details->private, null)) !== null)
{
$key = $this->key::load($private_key, $passphrase);
try
{
$key = $this->key::load(trim($private_key), $passphrase);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_LOADING_THE_PRIVATE_KEY_FILE_FAILED') . ': ' . $e->getMessage(),
'Error'
);
$key = null;
}
}
break;
case 4: // private key field
case 5: // both password and private key field
if (isset($this->details->private_key) && StringHelper::check($this->details->private_key))
{
$key = $this->key::load($this->details->private_key, $passphrase);
try
{
$key = $this->key::load(trim($this->details->private_key), $passphrase);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_LOADING_THE_PRIVATE_KEY_TEXT_FAILED') . ': ' . $e->getMessage(),
'Error'
);
$key = null;
}
}
break;
}
// remove any null bites from the username
$this->details->username = trim($this->details->username);
// login
if ((!empty($key) && !empty($password) && $sftp->login($this->details->username, $key, $password)) ||
(!empty($key) && $sftp->login($this->details->username, $key)))
if (!empty($key) && !empty($password))
{
return $sftp;
try
{
$sftp->login($this->details->username, $key, $password);
return $sftp;
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_LOGIN_FAILED') . ': ' . $e->getMessage(),
'Error'
);
}
}
elseif (!empty($key))
{
try
{
$sftp->login($this->details->username, $key);
return $sftp;
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('COM_COMPONENTBUILDER_LOGIN_FAILED') . ': ' . $e->getMessage(),
'Error'
);
}
}
}
return null;
}
}
}

View File

@ -22,16 +22,26 @@
**/
protected ?object $details = null;
/**
* Application object.
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/**
* Constructor
*
* @param KeyLoader $key The key loader object.
* @param CMSApplication|null $app The app object.
*
* @since 3.2.0
*/
public function __construct(KeyLoader $key)
public function __construct(KeyLoader $key, ?CMSApplication $app = null)
{
$this->key = $key;
$this->app = $app ?: Factory::getApplication();
}
/**
@ -77,10 +87,21 @@
StringHelper::check($this->details->path) &&
$this->details->path !== '/')
{
$path = '/' . trim((string) $this->details->path, '/');
$path = trim((string) $this->details->path);
$path = '/' . trim($path, '/') . '/';
}
return $this->client->put($path . '/' . $fileName, $data);
try
{
return $this->client->put($path . trim($fileName), $data);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::sprintf('Moving of the %s failed', $fileName) . ': ' . $e->getMessage(),
'Error'
);
}
}
return false;
@ -118,24 +139,23 @@
isset($this->details->username) && StringHelper::check($this->details->username))
{
// insure the port is set
$port = (isset($this->details->port) && is_numeric($this->details->port) && $this->details->port > 0)
? (int) $this->details->port : 22;
$port = (int)($this->details->port ?? 22);
// open the connection
$sftp = new SftpClient($this->details->host, $port);
// set the passphrase if it exist
$passphrase = $this->details->secret ?? null;
$passphrase = (isset($this->details->secret) && StringHelper::check(trim($this->details->secret))) ? trim($this->details->secret) : false;
// set the password if it exist
$password = $this->details->password ?? null;
$password = (isset($this->details->password) && StringHelper::check(trim($this->details->password))) ? trim($this->details->password) : false;
// now login based on authentication type
$key = null;
switch($this->details->authentication)
{
case 1: // password
$key = $this->details->password ?? null;
$key = $password ?? null;
$password = null;
break;
case 2: // private key file
@ -143,25 +163,75 @@
if (isset($this->details->private) && StringHelper::check($this->details->private) &&
($private_key = FileHelper::getContent($this->details->private, null)) !== null)
{
$key = $this->key::load($private_key, $passphrase);
try
{
$key = $this->key::load(trim($private_key), $passphrase);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Loading the private key file failed') . ': ' . $e->getMessage(),
'Error'
);
$key = null;
}
}
break;
case 4: // private key field
case 5: // both password and private key field
if (isset($this->details->private_key) && StringHelper::check($this->details->private_key))
{
$key = $this->key::load($this->details->private_key, $passphrase);
try
{
$key = $this->key::load(trim($this->details->private_key), $passphrase);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Loading the private key text failed') . ': ' . $e->getMessage(),
'Error'
);
$key = null;
}
}
break;
}
// remove any null bites from the username
$this->details->username = trim($this->details->username);
// login
if ((!empty($key) && !empty($password) && $sftp->login($this->details->username, $key, $password)) ||
(!empty($key) && $sftp->login($this->details->username, $key)))
if (!empty($key) && !empty($password))
{
return $sftp;
try
{
$sftp->login($this->details->username, $key, $password);
return $sftp;
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Login failed') . ': ' . $e->getMessage(),
'Error'
);
}
}
elseif (!empty($key))
{
try
{
$sftp->login($this->details->username, $key);
return $sftp;
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Login failed') . ': ' . $e->getMessage(),
'Error'
);
}
}
}
return null;
}
}

View File

@ -1,5 +1,5 @@
{
"add_head": "0",
"add_head": "1",
"add_licensing_template": "2",
"extends": "0",
"guid": "490b6aa5-5de7-4be5-a61a-f634f6e004c0",
@ -32,7 +32,7 @@
"namespace": "VDM\\Joomla\\Componentbuilder.Server.Sftp",
"description": "Sftp Class\r\n\r\n@since 3.2.0",
"licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 4th September, 2022\r\n * @author Llewellyn van der Merwe <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/joomla\/Component-Builder>\r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n",
"head": "",
"head": "use Joomla\\CMS\\Factory;\r\nuse Joomla\\CMS\\Language\\Text;\r\nuse Joomla\\CMS\\Application\\CMSApplication;",
"composer": {
"composer0": {
"access_point": "phpseclib3\/vendor\/autoload.php",

View File

@ -6,7 +6,16 @@
██║ ╚██████╔╝╚███╔███╔╝███████╗██║ ██║
╚═╝ ╚═════╝ ╚══╝╚══╝ ╚══════╝╚═╝ ╚═╝
```
> Error adding class diagram
# class KeyLoader (Details)
> namespace: **VDM\Joomla\Componentbuilder\Crypt**
> extends: **PublicKeyLoader**
```uml
@startuml
class KeyLoader #Gold {
}
@enduml
```
---
```

View File

@ -16,7 +16,7 @@ class Aes #Gold {
# int $size
+ __construct(BASEAES $aes, Random $random)
+ encrypt(string $string, string $key) : string
+ decrypt(string $string, string $key) : string
+ decrypt(string $string, string $key) : ?string
}
note right of Aes::__construct
@ -36,7 +36,7 @@ note right of Aes::decrypt
Decrypt a string as needed
since: 3.2.0
return: string
return: ?string
end note
@enduml

View File

@ -12,7 +12,9 @@
namespace VDM\Joomla\Componentbuilder\Crypt;
use phpseclib3\Crypt\AES as BASEAES;
use phpseclib3\Exception\BadDecryptionException;
use VDM\Joomla\Componentbuilder\Crypt\Random;
use VDM\Joomla\Componentbuilder\Interfaces\Cryptinterface;
@ -101,10 +103,10 @@ class Aes implements Cryptinterface
* @param string $string The string to decrypt
* @param string $key The decryption key
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
public function decrypt(string $string, string $key): ?string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
@ -124,8 +126,11 @@ class Aes implements Cryptinterface
// set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
return $this->aes->decrypt($string);
}
try {
return $this->aes->decrypt($string);
} catch (\Exception $ex) {
return null;
}
}
}

View File

@ -75,10 +75,10 @@
* @param string $string The string to decrypt
* @param string $key The decryption key
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
public function decrypt(string $string, string $key): ?string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
@ -98,5 +98,9 @@
// set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
return $this->aes->decrypt($string);
}
try {
return $this->aes->decrypt($string);
} catch (\Exception $ex) {
return null;
}
}

View File

@ -1,5 +1,5 @@
{
"add_head": "0",
"add_head": "1",
"add_licensing_template": "2",
"extends": "0",
"guid": "a25c82c8-14c2-40df-adae-f832709ab49b",
@ -27,6 +27,9 @@
"namespace": {
"namespace0": {
"use": "phpseclib3\\Crypt\\AES as BASEAES"
},
"namespace1": {
"use": "phpseclib3\\Exception\\BadDecryptionException"
}
}
}

View File

@ -6,7 +6,16 @@
██║ ╚██████╔╝╚███╔███╔╝███████╗██║ ██║
╚═╝ ╚═════╝ ╚══╝╚══╝ ╚══════╝╚═╝ ╚═╝
```
> Error adding class diagram
# class Random (Details)
> namespace: **VDM\Joomla\Componentbuilder\Crypt**
> extends: **CryptRandom**
```uml
@startuml
class Random #Gold {
}
@enduml
```
---
```

View File

@ -19,7 +19,7 @@ class Crypt #Gold {
# array $passwords
+ __construct(FOF $fof, Aes $aes, ...)
+ encrypt(string $string, string $method, ...) : string
+ decrypt(string $string, string $method, ...) : string
+ decrypt(string $string, string $method, ...) : ?string
+ exist(string $method) : bool
- getClassName(string $method) : ?string
- getClassNameFromRegistry(string $method) : ?string
@ -57,7 +57,7 @@ note right of Crypt::decrypt
Decrypt a string as needed
since: 3.2.0
return: string
return: ?string
arguments:
string $string

View File

@ -127,11 +127,11 @@ class Crypt
* @param string $method The encryption method to use
* @param string|null $default The default password
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $method,
?string $default = null): string
?string $default = null): ?string
{
if (($password = $this->getPassword($method, $default)) !== null
&& ($name = $this->getClassName($method)) !== null)
@ -139,7 +139,7 @@ class Crypt
return $this->{$name}->decrypt($string, $password);
}
return $string;
return null;
}
/**
@ -279,7 +279,6 @@ class Crypt
}
return $method;
}
}
}

View File

@ -98,11 +98,11 @@
* @param string $method The encryption method to use
* @param string|null $default The default password
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $method,
?string $default = null): string
?string $default = null): ?string
{
if (($password = $this->getPassword($method, $default)) !== null
&& ($name = $this->getClassName($method)) !== null)
@ -110,7 +110,7 @@
return $this->{$name}->decrypt($string, $password);
}
return $string;
return null;
}
/**
@ -250,4 +250,4 @@
}
return $method;
}
}

View File

@ -11,22 +11,10 @@
```uml
@startuml
class FOF #Gold {
# AES $aes
# Random $random
# int $size
+ __construct(AES $aes, Random $random)
+ encrypt(string $string, string $key) : string
+ decrypt(string $string, string $key) : string
# getExpandedKey(string $key, int $blockSize, ...) : string
# resizeKey(string $key, int $size) : ?string
+ decrypt(string $string, string $key) : ?string
}
note right of FOF::__construct
Constructor
since: 3.2.0
end note
note right of FOF::encrypt
Encrypt a string as needed
@ -37,33 +25,6 @@ end note
note right of FOF::decrypt
Decrypt a string as needed
since: 3.2.0
return: string
end note
note right of FOF::getExpandedKey
Function taken from FOFEncryptAes
changed a little but basically the same
to ensure we get the same passwords (not ideal)
we should use `$this->aes->setPassword(...)` instead
but can't for backwards compatibility issues with already encrypted string
since: 3.2.0
return: string
arguments:
string $key
int $blockSize
string $iv
end note
note right of FOF::resizeKey
Function taken from FOFEncryptAes
changed a little but basically the same
to ensure we get the same passwords (not ideal)
we should use `$this->aes->setPassword(...)` instead
but can't for backwards compatibility issues with already encrypted string
since: 3.2.0
return: ?string
end note

View File

@ -12,59 +12,17 @@
namespace VDM\Joomla\Componentbuilder\Crypt;
use phpseclib3\Crypt\AES;
use VDM\Joomla\Componentbuilder\Crypt\Random;
use VDM\Joomla\FOF\Encrypt\AES;
use VDM\Joomla\Componentbuilder\Interfaces\Cryptinterface;
/**
* Replacement Class for FOFEncryptAes
* Temp Class for FOFEncryptAes
*
* @since 3.2.0
*/
class FOF implements Cryptinterface
{
/**
* The Aes class
*
* @var AES
* @since 3.2.0
*/
protected AES $aes;
/**
* The Random class
*
* @var Random
* @since 3.2.0
*/
protected Random $random;
/**
* The block size
*
* @var int
* @since 3.2.0
*/
protected int $size = 128;
/**
* Constructor
*
* @param AES $aes The Aes class
* @param Random $random The Random class
*
* @since 3.2.0
*/
public function __construct(AES $aes, Random $random)
{
$this->aes = $aes;
$this->random = $random;
// we set the length once
$this->aes->setKeyLength($this->size);
}
/**
* Encrypt a string as needed
*
@ -76,19 +34,10 @@ class FOF implements Cryptinterface
**/
public function encrypt(string $string, string $key): string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// Get the encryption object.
$aes = new Aes($key, 128);
// get the IV value
$iv = $this->random::string($iv_length);
// Load the IV
$this->aes->setIV($iv);
// load the key
$this->aes->setKey($this->getExpandedKey($key, $iv_length, $iv));
// encrypt the string, and base 64 encode the result
return base64_encode($iv . $this->aes->encrypt($string));
return $aes->decryptString($string);
}
/**
@ -97,108 +46,19 @@ class FOF implements Cryptinterface
* @param string $string The string to decrypt
* @param string $key The decryption key
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
public function decrypt(string $string, string $key): ?string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// Get the encryption object.
$aes = new Aes($key, 128);
// remove base 64 encoding
$string = base64_decode($string);
// get the IV
$iv = substr($string, 0, $iv_length);
// remove the IV
$string = substr($string, $iv_length);
// set the key
$this->aes->setKey($this->getExpandedKey($key, $iv_length, $iv));
// set the IV
$this->aes->setIV($iv);
return $this->aes->decrypt($string);
}
/**
* Function taken from FOFEncryptAes
* changed a little but basically the same
* to ensure we get the same passwords (not ideal)
* we should use `$this->aes->setPassword(...)` instead
* but can't for backwards compatibility issues with already encrypted string
*
* @param string $key The key to expand
* @param int $blockSize The size of the block
* @param string $iv The IV used
*
* @return string
* @since 3.2.0
*/
protected function getExpandedKey(string $key, int $blockSize, string $iv): string
{
$pass_length = strlen($key);
if (function_exists('mb_strlen'))
{
$pass_length = mb_strlen($key, 'ASCII');
}
if ($pass_length != $blockSize)
{
$iterations = 1000;
$salt = $this->resizeKey($iv, 16);
$key = hash_pbkdf2('sha256', $key, $salt, $iterations, $blockSize, true);
}
return $key;
}
/**
* Function taken from FOFEncryptAes
* changed a little but basically the same
* to ensure we get the same passwords (not ideal)
* we should use `$this->aes->setPassword(...)` instead
* but can't for backwards compatibility issues with already encrypted string
*
* @param string $key The key to resize
* @param int $size The size of the block
*
* @return string|null
* @since 3.2.0
*/
protected function resizeKey(string $key, int $size): ?string
{
if (empty($key))
{
try {
return $aes->decryptString($string);
} catch (\Exception $ex) {
return null;
}
$key_length = strlen($key);
if (function_exists('mb_strlen'))
{
$key_length = mb_strlen($key, 'ASCII');
}
if ($key_length == $size)
{
return $key;
}
if ($key_length > $size)
{
if (function_exists('mb_substr'))
{
return mb_substr($key, 0, $size, 'ASCII');
}
return substr($key, 0, $size);
}
return $key . str_repeat("\0", ($size - $key_length));
}
}
}

View File

@ -1,44 +1,3 @@
/**
* The Aes class
*
* @var AES
* @since 3.2.0
*/
protected AES $aes;
/**
* The Random class
*
* @var Random
* @since 3.2.0
*/
protected Random $random;
/**
* The block size
*
* @var int
* @since 3.2.0
*/
protected int $size = 128;
/**
* Constructor
*
* @param AES $aes The Aes class
* @param Random $random The Random class
*
* @since 3.2.0
*/
public function __construct(AES $aes, Random $random)
{
$this->aes = $aes;
$this->random = $random;
// we set the length once
$this->aes->setKeyLength($this->size);
}
/**
* Encrypt a string as needed
*
@ -50,19 +9,10 @@
**/
public function encrypt(string $string, string $key): string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// Get the encryption object.
$aes = new Aes($key, 128);
// get the IV value
$iv = $this->random::string($iv_length);
// Load the IV
$this->aes->setIV($iv);
// load the key
$this->aes->setKey($this->getExpandedKey($key, $iv_length, $iv));
// encrypt the string, and base 64 encode the result
return base64_encode($iv . $this->aes->encrypt($string));
return $aes->decryptString($string);
}
/**
@ -71,105 +21,17 @@
* @param string $string The string to decrypt
* @param string $key The decryption key
*
* @return string
* @return string|null
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
public function decrypt(string $string, string $key): ?string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// Get the encryption object.
$aes = new Aes($key, 128);
// remove base 64 encoding
$string = base64_decode($string);
// get the IV
$iv = substr($string, 0, $iv_length);
// remove the IV
$string = substr($string, $iv_length);
// set the key
$this->aes->setKey($this->getExpandedKey($key, $iv_length, $iv));
// set the IV
$this->aes->setIV($iv);
return $this->aes->decrypt($string);
}
/**
* Function taken from FOFEncryptAes
* changed a little but basically the same
* to ensure we get the same passwords (not ideal)
* we should use `$this->aes->setPassword(...)` instead
* but can't for backwards compatibility issues with already encrypted string
*
* @param string $key The key to expand
* @param int $blockSize The size of the block
* @param string $iv The IV used
*
* @return string
* @since 3.2.0
*/
protected function getExpandedKey(string $key, int $blockSize, string $iv): string
{
$pass_length = strlen($key);
if (function_exists('mb_strlen'))
{
$pass_length = mb_strlen($key, 'ASCII');
}
if ($pass_length != $blockSize)
{
$iterations = 1000;
$salt = $this->resizeKey($iv, 16);
$key = hash_pbkdf2('sha256', $key, $salt, $iterations, $blockSize, true);
}
return $key;
}
/**
* Function taken from FOFEncryptAes
* changed a little but basically the same
* to ensure we get the same passwords (not ideal)
* we should use `$this->aes->setPassword(...)` instead
* but can't for backwards compatibility issues with already encrypted string
*
* @param string $key The key to resize
* @param int $size The size of the block
*
* @return string|null
* @since 3.2.0
*/
protected function resizeKey(string $key, int $size): ?string
{
if (empty($key))
{
try {
return $aes->decryptString($string);
} catch (\Exception $ex) {
return null;
}
$key_length = strlen($key);
if (function_exists('mb_strlen'))
{
$key_length = mb_strlen($key, 'ASCII');
}
if ($key_length == $size)
{
return $key;
}
if ($key_length > $size)
{
if (function_exists('mb_substr'))
{
return mb_substr($key, 0, $size, 'ASCII');
}
return substr($key, 0, $size);
}
return $key . str_repeat("\0", ($size - $key_length));
}
}

View File

@ -13,22 +13,13 @@
"type": "class",
"use_selection": {
"use_selection0": {
"use": "c46a42b4-b0d3-48e7-a6fa-af0399e1e66c",
"use": "99175f6d-dba8-4086-8a65-5c4ec175e61d",
"as": "default"
}
},
"namespace": "VDM\\Joomla\\Componentbuilder.Crypt.FOF",
"description": "Replacement Class for FOFEncryptAes\r\n\r\n@since 3.2.0",
"description": "Temp Class for FOFEncryptAes\r\n\r\n@since 3.2.0",
"licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 4th September, 2022\r\n * @author Llewellyn van der Merwe <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/joomla\/Component-Builder>\r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n",
"head": "",
"composer": {
"composer0": {
"access_point": "phpseclib3\/vendor\/autoload.php",
"namespace": {
"namespace0": {
"use": "phpseclib3\\Crypt\\AES"
}
}
}
}
"composer": ""
}

View File

@ -8,6 +8,7 @@
```
# class Load (Details)
> namespace: **VDM\Joomla\Componentbuilder\Server\Model**
> extends: **Model**
```uml
@startuml
class Load #Gold {