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 untrusted user: Robot
GPG Key ID: 14DECD44E7E1BB95
24 changed files with 295 additions and 438 deletions

View File

@ -13,6 +13,7 @@
class Ftp #Gold { class Ftp #Gold {
# ?FtpClient $client # ?FtpClient $client
# ?object $details # ?object $details
+ iables $signature
+ set(object $details) : Ftp + set(object $details) : Ftp
+ move(string $localPath, string $fileName) : bool + move(string $localPath, string $fileName) : bool
- connected() : 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 // make sure we have a string and it is not default or empty
if (StringHelper::check($this->details->signature)) if (StringHelper::check($this->details->signature))
{ {
// turn into variables // turn into array of variables
parse_str((string) $this->details->signature); $signature = [];
parse_str((string) $this->details->signature, $signature);
// set options // 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) if ('timeout' === $o__p0t1on)
{ {
@ -136,10 +137,10 @@ class Ftp implements Serverinterface
$options = []; $options = [];
} }
// get ftp object // get ftp object
if (isset($host) && $host != 'HOSTNAME' && if (isset($signature['host']) && $signature['host'] != 'HOSTNAME' &&
isset($port) && $port != 'PORT_INT' && isset($signature['port']) && $signature['port'] != 'PORT_INT' &&
isset($username) && $username != 'user@name.com' && isset($signature['username']) && $signature['username'] != 'user@name.com' &&
isset($password) && $password != 'password') isset($signature['password']) && $signature['password'] != 'password')
{ {
// this is a singleton // this is a singleton
return FtpClient::getInstance($host, $port, $options, $username, $password); 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 // make sure we have a string and it is not default or empty
if (StringHelper::check($this->details->signature)) if (StringHelper::check($this->details->signature))
{ {
// turn into variables // turn into array of variables
parse_str((string) $this->details->signature); $signature = [];
parse_str((string) $this->details->signature, $signature);
// set options // 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) if ('timeout' === $o__p0t1on)
{ {
@ -108,10 +109,10 @@
$options = []; $options = [];
} }
// get ftp object // get ftp object
if (isset($host) && $host != 'HOSTNAME' && if (isset($signature['host']) && $signature['host'] != 'HOSTNAME' &&
isset($port) && $port != 'PORT_INT' && isset($signature['port']) && $signature['port'] != 'PORT_INT' &&
isset($username) && $username != 'user@name.com' && isset($signature['username']) && $signature['username'] != 'user@name.com' &&
isset($password) && $password != 'password') isset($signature['password']) && $signature['password'] != 'password')
{ {
// this is a singleton // this is a singleton
return FtpClient::getInstance($host, $port, $options, $username, $password); return FtpClient::getInstance($host, $port, $options, $username, $password);

View File

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

View File

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

View File

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

View File

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

View File

@ -22,16 +22,26 @@
**/ **/
protected ?object $details = null; protected ?object $details = null;
/**
* Application object.
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/** /**
* Constructor * Constructor
* *
* @param KeyLoader $key The key loader object. * @param KeyLoader $key The key loader object.
* @param CMSApplication|null $app The app object.
* *
* @since 3.2.0 * @since 3.2.0
*/ */
public function __construct(KeyLoader $key) public function __construct(KeyLoader $key, ?CMSApplication $app = null)
{ {
$this->key = $key; $this->key = $key;
$this->app = $app ?: Factory::getApplication();
} }
/** /**
@ -77,10 +87,21 @@
StringHelper::check($this->details->path) && StringHelper::check($this->details->path) &&
$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; return false;
@ -118,24 +139,23 @@
isset($this->details->username) && StringHelper::check($this->details->username)) isset($this->details->username) && StringHelper::check($this->details->username))
{ {
// insure the port is set // insure the port is set
$port = (isset($this->details->port) && is_numeric($this->details->port) && $this->details->port > 0) $port = (int)($this->details->port ?? 22);
? (int) $this->details->port : 22;
// open the connection // open the connection
$sftp = new SftpClient($this->details->host, $port); $sftp = new SftpClient($this->details->host, $port);
// set the passphrase if it exist // 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 // 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 // now login based on authentication type
$key = null; $key = null;
switch($this->details->authentication) switch($this->details->authentication)
{ {
case 1: // password case 1: // password
$key = $this->details->password ?? null; $key = $password ?? null;
$password = null; $password = null;
break; break;
case 2: // private key file case 2: // private key file
@ -143,24 +163,74 @@
if (isset($this->details->private) && StringHelper::check($this->details->private) && if (isset($this->details->private) && StringHelper::check($this->details->private) &&
($private_key = FileHelper::getContent($this->details->private, null)) !== null) ($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; break;
case 4: // private key field case 4: // private key field
case 5: // both password and private key field case 5: // both password and private key field
if (isset($this->details->private_key) && StringHelper::check($this->details->private_key)) 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; break;
} }
// remove any null bites from the username
$this->details->username = trim($this->details->username);
// login // login
if ((!empty($key) && !empty($password) && $sftp->login($this->details->username, $key, $password)) || if (!empty($key) && !empty($password))
(!empty($key) && $sftp->login($this->details->username, $key)))
{ {
try
{
$sftp->login($this->details->username, $key, $password);
return $sftp; 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; return null;

View File

@ -1,5 +1,5 @@
{ {
"add_head": "0", "add_head": "1",
"add_licensing_template": "2", "add_licensing_template": "2",
"extends": "0", "extends": "0",
"guid": "490b6aa5-5de7-4be5-a61a-f634f6e004c0", "guid": "490b6aa5-5de7-4be5-a61a-f634f6e004c0",
@ -32,7 +32,7 @@
"namespace": "VDM\\Joomla\\Componentbuilder.Server.Sftp", "namespace": "VDM\\Joomla\\Componentbuilder.Server.Sftp",
"description": "Sftp Class\r\n\r\n@since 3.2.0", "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", "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": { "composer": {
"composer0": { "composer0": {
"access_point": "phpseclib3\/vendor\/autoload.php", "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 # int $size
+ __construct(BASEAES $aes, Random $random) + __construct(BASEAES $aes, Random $random)
+ encrypt(string $string, string $key) : string + encrypt(string $string, string $key) : string
+ decrypt(string $string, string $key) : string + decrypt(string $string, string $key) : ?string
} }
note right of Aes::__construct note right of Aes::__construct
@ -36,7 +36,7 @@ note right of Aes::decrypt
Decrypt a string as needed Decrypt a string as needed
since: 3.2.0 since: 3.2.0
return: string return: ?string
end note end note
@enduml @enduml

View File

@ -12,7 +12,9 @@
namespace VDM\Joomla\Componentbuilder\Crypt; namespace VDM\Joomla\Componentbuilder\Crypt;
use phpseclib3\Crypt\AES as BASEAES; use phpseclib3\Crypt\AES as BASEAES;
use phpseclib3\Exception\BadDecryptionException;
use VDM\Joomla\Componentbuilder\Crypt\Random; use VDM\Joomla\Componentbuilder\Crypt\Random;
use VDM\Joomla\Componentbuilder\Interfaces\Cryptinterface; use VDM\Joomla\Componentbuilder\Interfaces\Cryptinterface;
@ -101,10 +103,10 @@ class Aes implements Cryptinterface
* @param string $string The string to decrypt * @param string $string The string to decrypt
* @param string $key The decryption key * @param string $key The decryption key
* *
* @return string * @return string|null
* @since 3.2.0 * @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 // we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3; $iv_length = (int) $this->aes->getBlockLength() >> 3;
@ -124,8 +126,11 @@ class Aes implements Cryptinterface
// set the password // set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt'); $this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
try {
return $this->aes->decrypt($string); 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 $string The string to decrypt
* @param string $key The decryption key * @param string $key The decryption key
* *
* @return string * @return string|null
* @since 3.2.0 * @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 // we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3; $iv_length = (int) $this->aes->getBlockLength() >> 3;
@ -98,5 +98,9 @@
// set the password // set the password
$this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt'); $this->aes->setPassword($key, 'pbkdf2', 'sha256', 'VastDevelopmentMethod/salt');
try {
return $this->aes->decrypt($string); 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", "add_licensing_template": "2",
"extends": "0", "extends": "0",
"guid": "a25c82c8-14c2-40df-adae-f832709ab49b", "guid": "a25c82c8-14c2-40df-adae-f832709ab49b",
@ -27,6 +27,9 @@
"namespace": { "namespace": {
"namespace0": { "namespace0": {
"use": "phpseclib3\\Crypt\\AES as BASEAES" "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 # array $passwords
+ __construct(FOF $fof, Aes $aes, ...) + __construct(FOF $fof, Aes $aes, ...)
+ encrypt(string $string, string $method, ...) : string + encrypt(string $string, string $method, ...) : string
+ decrypt(string $string, string $method, ...) : string + decrypt(string $string, string $method, ...) : ?string
+ exist(string $method) : bool + exist(string $method) : bool
- getClassName(string $method) : ?string - getClassName(string $method) : ?string
- getClassNameFromRegistry(string $method) : ?string - getClassNameFromRegistry(string $method) : ?string
@ -57,7 +57,7 @@ note right of Crypt::decrypt
Decrypt a string as needed Decrypt a string as needed
since: 3.2.0 since: 3.2.0
return: string return: ?string
arguments: arguments:
string $string string $string

View File

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

View File

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

View File

@ -11,22 +11,10 @@
```uml ```uml
@startuml @startuml
class FOF #Gold { class FOF #Gold {
# AES $aes
# Random $random
# int $size
+ __construct(AES $aes, Random $random)
+ encrypt(string $string, string $key) : string + encrypt(string $string, string $key) : string
+ decrypt(string $string, string $key) : string + decrypt(string $string, string $key) : ?string
# getExpandedKey(string $key, int $blockSize, ...) : string
# resizeKey(string $key, int $size) : ?string
} }
note right of FOF::__construct
Constructor
since: 3.2.0
end note
note right of FOF::encrypt note right of FOF::encrypt
Encrypt a string as needed Encrypt a string as needed
@ -37,33 +25,6 @@ end note
note right of FOF::decrypt note right of FOF::decrypt
Decrypt a string as needed 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 since: 3.2.0
return: ?string return: ?string
end note end note

View File

@ -12,59 +12,17 @@
namespace VDM\Joomla\Componentbuilder\Crypt; namespace VDM\Joomla\Componentbuilder\Crypt;
use phpseclib3\Crypt\AES; use VDM\Joomla\FOF\Encrypt\AES;
use VDM\Joomla\Componentbuilder\Crypt\Random;
use VDM\Joomla\Componentbuilder\Interfaces\Cryptinterface; use VDM\Joomla\Componentbuilder\Interfaces\Cryptinterface;
/** /**
* Replacement Class for FOFEncryptAes * Temp Class for FOFEncryptAes
* *
* @since 3.2.0 * @since 3.2.0
*/ */
class FOF implements Cryptinterface 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 * Encrypt a string as needed
* *
@ -76,19 +34,10 @@ class FOF implements Cryptinterface
**/ **/
public function encrypt(string $string, string $key): string public function encrypt(string $string, string $key): string
{ {
// we get the IV length // Get the encryption object.
$iv_length = (int) $this->aes->getBlockLength() >> 3; $aes = new Aes($key, 128);
// get the IV value return $aes->decryptString($string);
$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));
} }
/** /**
@ -97,108 +46,19 @@ class FOF implements Cryptinterface
* @param string $string The string to decrypt * @param string $string The string to decrypt
* @param string $key The decryption key * @param string $key The decryption key
* *
* @return string
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// 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 * @return string|null
* @since 3.2.0 * @since 3.2.0
*/ **/
protected function resizeKey(string $key, int $size): ?string public function decrypt(string $string, string $key): ?string
{
if (empty($key))
{ {
// Get the encryption object.
$aes = new Aes($key, 128);
try {
return $aes->decryptString($string);
} catch (\Exception $ex) {
return null; 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 * Encrypt a string as needed
* *
@ -50,19 +9,10 @@
**/ **/
public function encrypt(string $string, string $key): string public function encrypt(string $string, string $key): string
{ {
// we get the IV length // Get the encryption object.
$iv_length = (int) $this->aes->getBlockLength() >> 3; $aes = new Aes($key, 128);
// get the IV value return $aes->decryptString($string);
$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));
} }
/** /**
@ -71,105 +21,17 @@
* @param string $string The string to decrypt * @param string $string The string to decrypt
* @param string $key The decryption key * @param string $key The decryption key
* *
* @return string
* @since 3.2.0
**/
public function decrypt(string $string, string $key): string
{
// we get the IV length
$iv_length = (int) $this->aes->getBlockLength() >> 3;
// 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 * @return string|null
* @since 3.2.0 * @since 3.2.0
*/ **/
protected function resizeKey(string $key, int $size): ?string public function decrypt(string $string, string $key): ?string
{
if (empty($key))
{ {
// Get the encryption object.
$aes = new Aes($key, 128);
try {
return $aes->decryptString($string);
} catch (\Exception $ex) {
return null; 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", "type": "class",
"use_selection": { "use_selection": {
"use_selection0": { "use_selection0": {
"use": "c46a42b4-b0d3-48e7-a6fa-af0399e1e66c", "use": "99175f6d-dba8-4086-8a65-5c4ec175e61d",
"as": "default" "as": "default"
} }
}, },
"namespace": "VDM\\Joomla\\Componentbuilder.Crypt.FOF", "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", "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": "",
"composer": { "composer": ""
"composer0": {
"access_point": "phpseclib3\/vendor\/autoload.php",
"namespace": {
"namespace0": {
"use": "phpseclib3\\Crypt\\AES"
}
}
}
}
} }

View File

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