From 39ad665bec6703f99198d493bfe57d4ea973bfbb Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Thu, 28 Sep 2023 14:29:50 +0200 Subject: [PATCH] Key SSL --- component/Key/Ssl.php | 56 ++++++++++++++++++ test/Key/SslTest.php | 133 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 component/Key/Ssl.php create mode 100644 test/Key/SslTest.php diff --git a/component/Key/Ssl.php b/component/Key/Ssl.php new file mode 100644 index 0000000..17f7bbf --- /dev/null +++ b/component/Key/Ssl.php @@ -0,0 +1,56 @@ +private + ); + } catch (\Exception $e) { + $success = false; + } + + if (!$success) { + throw new NotCompliant('Encryption invalid, verify private key'); + } + + return $encryptedData; + } + + public function decrypt( + string $encryptedData + ): string { + try { + $success = openssl_public_decrypt( + $encryptedData, + $data, + $this->public + ); + } catch (\Exception $e) { + $success = false; + } + + if (!$success) { + throw new NotCompliant('Decryption invalid, verify encrypted data or public key'); + } + + return $data; + } +} diff --git a/test/Key/SslTest.php b/test/Key/SslTest.php new file mode 100644 index 0000000..a0d5376 --- /dev/null +++ b/test/Key/SslTest.php @@ -0,0 +1,133 @@ +fixture = new Ssl( + self::PRIVATE, + self::PUBLIC + ); + $this->fixtureInvalid = new Ssl( + self::INVALID_PRIVATE, + self::INVALID_PUBLIC + ); + } + + public function testGetPrivate(): void + { + $result = $this->fixture->private; + + $this->assertIsString($result); + } + + public function testGetPublic(): void + { + $result = $this->fixture->public; + + $this->assertIsString($result); + } + + public function testEncrypt(): void + { + $result = $this->fixture->encrypt('Foo bar'); + + $this->assertIsString($result); + } + + public function testEncryptInvalid(): void + { + $this->expectException(NotCompliant::class); + + $result = $this->fixtureInvalid->encrypt('Foo bar'); + } + + public function testEncryptInvalidBis(): void + { + $this->expectException(NotCompliant::class); + + $result = $this->fixtureInvalid->encrypt(''); + } + + public function testDecrypt(): void + { + $result = $this->fixture->decrypt( + $this->fixture->encrypt('Foo bar') + ); + + $this->assertIsString($result); + } + + public function testDecryptInvalid(): void + { + $this->expectException(NotCompliant::class); + + $result = $this->fixtureInvalid->decrypt( + $this->fixture->encrypt('Foo bar') + ); + } +}