|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Cryptography\Tests\Signing; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Tempest\Cryptography\Signing\Exceptions\SigningKeyWasMissing; |
| 7 | +use Tempest\Cryptography\Signing\GenericSigner; |
| 8 | +use Tempest\Cryptography\Signing\SigningAlgorithm; |
| 9 | +use Tempest\Cryptography\Signing\SigningConfig; |
| 10 | + |
| 11 | +final class SignerTest extends TestCase |
| 12 | +{ |
| 13 | + public function test_good_signature(): void |
| 14 | + { |
| 15 | + $signer = new GenericSigner(new SigningConfig( |
| 16 | + algorithm: SigningAlgorithm::SHA256, |
| 17 | + key: 'my_secret_key', |
| 18 | + )); |
| 19 | + |
| 20 | + $data = 'important data'; |
| 21 | + $signature = $signer->sign($data); |
| 22 | + |
| 23 | + $this->assertTrue($signer->verify($data, $signature)); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_bad_signature(): void |
| 27 | + { |
| 28 | + $signer = new GenericSigner(new SigningConfig( |
| 29 | + algorithm: SigningAlgorithm::SHA256, |
| 30 | + key: 'my_secret_key', |
| 31 | + )); |
| 32 | + |
| 33 | + $data = 'important data'; |
| 34 | + $signature = $signer->sign($data); |
| 35 | + |
| 36 | + // Tamper with the data |
| 37 | + $tamperedData = 'tampered data'; |
| 38 | + |
| 39 | + $this->assertFalse($signer->verify($tamperedData, $signature)); |
| 40 | + } |
| 41 | + |
| 42 | + public function test_different_algoritms(): void |
| 43 | + { |
| 44 | + $signer1 = new GenericSigner(new SigningConfig( |
| 45 | + algorithm: SigningAlgorithm::SHA256, |
| 46 | + key: 'my_secret_key', |
| 47 | + )); |
| 48 | + |
| 49 | + $signer2 = new GenericSigner(new SigningConfig( |
| 50 | + algorithm: SigningAlgorithm::SHA512, |
| 51 | + key: 'my_secret_key', |
| 52 | + )); |
| 53 | + |
| 54 | + $data = 'important data'; |
| 55 | + $signature1 = $signer1->sign($data); |
| 56 | + $signature2 = $signer2->sign($data); |
| 57 | + |
| 58 | + // Signatures should be different due to different algorithms |
| 59 | + $this->assertNotEquals($signature1, $signature2); |
| 60 | + |
| 61 | + // Verify with the correct signer |
| 62 | + $this->assertTrue($signer1->verify($data, $signature1)); |
| 63 | + $this->assertTrue($signer2->verify($data, $signature2)); |
| 64 | + |
| 65 | + // Verify with the wrong signer |
| 66 | + $this->assertFalse($signer1->verify($data, $signature2)); |
| 67 | + $this->assertFalse($signer2->verify($data, $signature1)); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_no_signing_key(): void |
| 71 | + { |
| 72 | + $this->expectException(SigningKeyWasMissing::class); |
| 73 | + |
| 74 | + $signer = new GenericSigner(new SigningConfig( |
| 75 | + algorithm: SigningAlgorithm::SHA256, |
| 76 | + key: '', |
| 77 | + )); |
| 78 | + |
| 79 | + $signer->sign('important data'); |
| 80 | + } |
| 81 | + |
| 82 | + public function test_empty_data(): void |
| 83 | + { |
| 84 | + $signer = new GenericSigner(new SigningConfig( |
| 85 | + algorithm: SigningAlgorithm::SHA256, |
| 86 | + key: 'my_secret_key', |
| 87 | + )); |
| 88 | + |
| 89 | + $signature = $signer->sign(''); |
| 90 | + |
| 91 | + // An empty string should still produce a valid signature |
| 92 | + $this->assertTrue($signer->verify('', $signature)); |
| 93 | + } |
| 94 | + |
| 95 | + public function test_consistent_signature(): void |
| 96 | + { |
| 97 | + $signer = new GenericSigner(new SigningConfig( |
| 98 | + algorithm: SigningAlgorithm::SHA256, |
| 99 | + key: 'my_secret_key', |
| 100 | + )); |
| 101 | + |
| 102 | + $data = 'important data'; |
| 103 | + $signature1 = $signer->sign($data); |
| 104 | + $signature2 = $signer->sign($data); |
| 105 | + |
| 106 | + // Signing the same data should produce the same signature |
| 107 | + $this->assertEquals($signature1, $signature2); |
| 108 | + } |
| 109 | + |
| 110 | + public function test_different_keys(): void |
| 111 | + { |
| 112 | + $signer1 = new GenericSigner(new SigningConfig( |
| 113 | + algorithm: SigningAlgorithm::SHA256, |
| 114 | + key: 'signer1_key_foo', |
| 115 | + )); |
| 116 | + |
| 117 | + $signer2 = new GenericSigner(new SigningConfig( |
| 118 | + algorithm: SigningAlgorithm::SHA512, |
| 119 | + key: 'signer2_key_bar', |
| 120 | + )); |
| 121 | + |
| 122 | + $data = 'important data'; |
| 123 | + $signature1 = $signer1->sign($data); |
| 124 | + $signature2 = $signer2->sign($data); |
| 125 | + |
| 126 | + // Signatures should be different due to different keys |
| 127 | + $this->assertNotEquals($signature1, $signature2); |
| 128 | + |
| 129 | + // Verify with the correct signer |
| 130 | + $this->assertTrue($signer1->verify($data, $signature1)); |
| 131 | + $this->assertTrue($signer2->verify($data, $signature2)); |
| 132 | + |
| 133 | + // Verify with the wrong signer |
| 134 | + $this->assertFalse($signer1->verify($data, $signature2)); |
| 135 | + $this->assertFalse($signer2->verify($data, $signature1)); |
| 136 | + } |
| 137 | +} |
0 commit comments