From 4c80ba15430d0249dfc1741598a1961f50e6693a Mon Sep 17 00:00:00 2001 From: tracer Date: Thu, 22 Sep 2022 19:31:38 +0200 Subject: [PATCH] added encryption --- src/Entity/Panel.php | 68 +++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/src/Entity/Panel.php b/src/Entity/Panel.php index 5bc70e4..cb965b1 100644 --- a/src/Entity/Panel.php +++ b/src/Entity/Panel.php @@ -2,6 +2,11 @@ namespace App\Entity; +use App\Controller\ConfigController; +use App\Controller\EncryptionController; +use Exception; +use SodiumException; + /** * */ @@ -12,22 +17,45 @@ class Panel * @param int $id * @param string $a * @param string $aaaa + * @param string $passphrase * @param string $apikey * @param string $apikeyPrefix * @param string $self */ public function __construct( - - private string $name, - private int $id = 0, - private string $a = '', - private string $aaaa = '', - private string $apikey = '', - private string $apikeyPrefix = '', - private string $self = 'no', + private string $name, + private int $id = 0, + private string $a = '', + private string $aaaa = '', + private readonly string $passphrase = '', + private string $apikey = '', + private string $apikeyPrefix = '', + private string $self = 'no', ) - {} + { + if ($this->passphrase) { + $configController = new ConfigController(); + $encryptionController = new EncryptionController(); + $encryptionKey = $configController->getConfig(configKey: 'encryptionKey'); + + $this->apikeyPrefix = strtok(string: $this->passphrase, token: '.'); + + try { + $this->apikey = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey); + } catch (Exception|SodiumException $e) { + die($e->getMessage() . PHP_EOL); + } + } + } + + /** + * @return string + */ + public function getPassphrase(): string + { + return $this->passphrase; + } /** @@ -88,7 +116,6 @@ class Panel } - /** * @return string */ @@ -102,21 +129,10 @@ class Panel */ public function getApikey(): string { - return $this->apikey; - } - - - - /** - * @param string $apikey - */ - public function setApikey(string $apikey): void - { - $this->apikey = $apikey; + return $this->apikey; } - /** * @param string $a */ @@ -142,12 +158,4 @@ class Panel return $this->apikeyPrefix; } - /** - * @param string $apikeyPrefix - */ - public function setApikeyPrefix(string $apikeyPrefix): void - { - $this->apikeyPrefix = $apikeyPrefix; - } - }