diff --git a/src/Crypto/RsaKeypair.php b/src/Crypto/RsaKeypair.php index cab7f6b..9ec1f47 100644 --- a/src/Crypto/RsaKeypair.php +++ b/src/Crypto/RsaKeypair.php @@ -1,6 +1,7 @@ privateKey ) { + if ( empty( $this->privateKey ) ) { throw new BadMethodCallException( 'Unable to sign data without a private key' ); @@ -95,7 +96,7 @@ class RsaKeypair */ public function fromPublicKey( string $publicKey ) { - return new RsaKeypair( $publicKey, null ); + return new RsaKeypair( $publicKey, '' ); } } ?> diff --git a/test/RsaKeypairTest.php b/test/RsaKeypairTest.php index fe6ed9c..568db9b 100644 --- a/test/RsaKeypairTest.php +++ b/test/RsaKeypairTest.php @@ -1,8 +1,10 @@ assertStringStartsWith( '-----BEGIN PUBLIC KEY-----', $keypair->getPublicKey() ); $this->assertStringEndsWith( '-----END PUBLIC KEY-----', $keypair->getPublicKey() ); } + + public function testItSignsAndValidatesSignatures() + { + $keypair = RsaKeypair::generate(); + $data = 'This is some data'; + $signature = $keypair->sign( $data ); + $this->assertInternalType( 'string', $signature ); + $this->assertNotEmpty( $signature ); + $verified = $keypair->verify( $data, $signature ); + $this->assertTrue( $verified ); + } + + public function testItGivesErrorValidatingInvalidSignature() + { + $keypair = RsaKeypair::generate(); + $data = 'This is some data'; + $signature = 'not a real signature'; + $this->expectException( Error::class ); + $verified = $keypair->verify( $data, $signature ); + } + + public function testItReturnsNotVerifiedForValidButWrongSignature() + { + $keypairOne = RsaKeypair::generate(); + $data = 'This is some data'; + $signature = $keypairOne->sign( $data ); + $keypairTwo = RsaKeypair::generate(); + $verified = $keypairTwo->verify( $data, $signature ); + $this->assertFalse( $verified ); + } + + public function testItCreatesValidPublicKeyOnly() + { + $fullKeypair = RsaKeypair::generate(); + $publicKeyOnly = RsaKeypair::fromPublicKey( $fullKeypair->getPublicKey() ); + $data = 'This is some data'; + $signature = $fullKeypair->sign( $data ); + $verified = $publicKeyOnly->verify( $data, $signature ); + $this->assertTrue( $verified ); + } + + public function testItCannotSignWithPublicKeyOnly() + { + $fullKeypair = RsaKeypair::generate(); + $publicKeyOnly = RsaKeypair::fromPublicKey( $fullKeypair->getPublicKey() ); + $data = 'This is some data'; + $this->expectException( BadMethodCallException::class ); + $this->expectExceptionMessage( 'Unable to sign data without a private key' ); + $signature = $publicKeyOnly->sign( $data ); + } + + public function testItSignsAndVerifiesEmptyData() + { + $keypair = RsaKeypair::generate(); + $data = ''; + $signature = $keypair->sign( $data ); + $verified = $keypair->verify( $data, $signature ); + $this->assertTrue( $verified ); + } + + public function testItHandlesInvalidPublicKeyOnly() + { + $fullKeypair = RsaKeypair::generate(); + $publicKeyOnly = RsaKeypair::fromPublicKey( 'not a real public key' ); + $data = 'This is some data'; + $signature = $fullKeypair->sign( $data ); + $verified = $publicKeyOnly->verify( $data, $signature ); + $this->assertFalse( $verified ); + } } ?>