From 172398307227680161293629dcc13b79e6c775a1 Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 4 Mar 2025 13:41:44 -0500 Subject: [PATCH] Add getSharedSecret method for Diffie-Hellman key exchange in EncryptionChannelSecret --- .../Objects/Client/EncryptionChannelSecret.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Socialbox/Objects/Client/EncryptionChannelSecret.php b/src/Socialbox/Objects/Client/EncryptionChannelSecret.php index 7aacaad..d631910 100644 --- a/src/Socialbox/Objects/Client/EncryptionChannelSecret.php +++ b/src/Socialbox/Objects/Client/EncryptionChannelSecret.php @@ -2,6 +2,8 @@ namespace Socialbox\Objects\Client; + use Socialbox\Classes\Cryptography; + use Socialbox\Exceptions\CryptographyException; use Socialbox\Interfaces\SerializableInterface; use Socialbox\Objects\PeerAddress; @@ -87,6 +89,22 @@ $this->receivingPublicEncryptionKey = $receivingPublicEncryptionKey; } + /** + * Preform a Diffie-Hellman Exchange to get the shared secret between the two peers + * + * @return string The shared secret + * @throws CryptographyException If the receiving public encryption key is not set + */ + public function getSharedSecret(): string + { + if($this->receivingPublicEncryptionKey === null) + { + throw new CryptographyException('The receiving public encryption key is not set'); + } + + return Cryptography::performDHE($this->receivingPublicEncryptionKey, $this->localPrivateEncryptionKey); + } + /** * @inheritDoc */