diff --git a/Tests/NativeTypesTest.php b/Tests/NativeTypesTest.php new file mode 100644 index 00000000..0a720f25 --- /dev/null +++ b/Tests/NativeTypesTest.php @@ -0,0 +1,85 @@ + [Crypt::class, 'decrypt', 'string'], + 'Crypt::encrypt()' => [Crypt::class, 'encrypt', 'string'], + 'Crypt::generateKey()' => [Crypt::class, 'generateKey', Key::class], + 'Crypt::setKey()' => [Crypt::class, 'setKey', 'static'], + 'Crypt::genRandomBytes()' => [Crypt::class, 'genRandomBytes', 'string'], + ]; + + foreach ([CipherInterface::class, Crypto::class, OpenSSL::class, Sodium::class] as $class) { + $short = substr($class, strrpos($class, chr(92)) + 1); + + $cases[$short . '::decrypt()'] = [$class, 'decrypt', 'string']; + $cases[$short . '::encrypt()'] = [$class, 'encrypt', 'string']; + $cases[$short . '::generateKey()'] = [$class, 'generateKey', Key::class]; + } + + return $cases; + } + + /** + * @testdox The public API declares native return types + * + * @param string $class The class name + * @param string $method The method name + * @param string $type The expected return type + */ + #[DataProvider('dataReturnTypes')] + public function testReturnType(string $class, string $method, string $type) + { + $this->assertSame($type, (string) (new \ReflectionMethod($class, $method))->getReturnType()); + } + + /** + * @testdox Non-string data is rejected instead of being coerced + */ + public function testNullDataIsRejected() + { + $cipher = new Crypto(); + $crypt = new Crypt($cipher, $cipher->generateKey()); + + $this->expectException(\TypeError::class); + + $crypt->encrypt(null); + } + + /** + * @testdox The length of random bytes must be an integer + */ + public function testGenRandomBytesRequiresAnInteger() + { + $this->expectException(\TypeError::class); + + Crypt::genRandomBytes('sixteen'); + } +} diff --git a/docs/index.md b/docs/index.md index 805bd724..45534879 100644 --- a/docs/index.md +++ b/docs/index.md @@ -3,3 +3,4 @@ * Upgrading * [Updating from v2 to v3](v2-to-v3-update.md) * [Updating from v3 to v4](v3-to-v4-update.md) + * [Updating from v4 to v5](v4-to-v5-update.md) diff --git a/docs/v4-to-v5-update.md b/docs/v4-to-v5-update.md new file mode 100644 index 00000000..745b62c1 --- /dev/null +++ b/docs/v4-to-v5-update.md @@ -0,0 +1,43 @@ +# Updating from v4 to v5 + +Release 5.0.0 removes API that was deprecated in 4.x and adds native types to the public API. Each +breaking change has its own section below. + + + + +## Native parameter and return types + +The cipher API and `Crypt` now declare native types: + +| Method | v4 | v5 | +|---|---|---| +| `CipherInterface::encrypt()` | `encrypt($data, Key $key)` | `encrypt(string $data, Key $key): string` | +| `CipherInterface::decrypt()` | `decrypt($data, Key $key)` | `decrypt(string $data, Key $key): string` | +| `CipherInterface::generateKey()` | `generateKey(array $options = [])` | `generateKey(array $options = []): Key` | +| `Crypt::encrypt()` | `encrypt($data)` | `encrypt(string $data): string` | +| `Crypt::decrypt()` | `decrypt($data)` | `decrypt(string $data): string` | +| `Crypt::generateKey()` | `generateKey(array $options = [])` | `generateKey(array $options = []): Key` | +| `Crypt::setKey()` | `setKey(Key $key)` | `setKey(Key $key): static` | +| `Crypt::genRandomBytes()` | `genRandomBytes($length = 16)` | `genRandomBytes(int $length = 16): string` | + +`Cipher\Crypto`, `Cipher\OpenSSL` and `Cipher\Sodium` implement the new interface signatures. + +**What to change:** + +* Your own `CipherInterface` implementations must declare the same parameter and return types. +* Subclasses of `Crypt` or of the bundled ciphers that override these methods must add the return + types. +* `null` is no longer accepted as data: `encrypt(null)` throws a `TypeError` instead of being + treated as an empty string. Without `declare(strict_types=1)`, integers and floats are still + converted to strings. + + + + + +## Dependency changes + +| Package | v4 (4.0.0) | v5 (5.0.0) | +|---|---|---| +| `php` | `^8.3.0` | `^8.3.0` | diff --git a/src/Cipher/Crypto.php b/src/Cipher/Crypto.php index a1c19369..fcd9eb03 100644 --- a/src/Cipher/Crypto.php +++ b/src/Cipher/Crypto.php @@ -40,7 +40,7 @@ class Crypto implements CipherInterface * @throws DecryptionException if the data cannot be decrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function decrypt($data, Key $key) + public function decrypt(string $data, Key $key): string { // Validate key. if ($key->getType() !== 'crypto') { @@ -69,7 +69,7 @@ public function decrypt($data, Key $key) * @throws EncryptionException if the data cannot be encrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function encrypt($data, Key $key) + public function encrypt(string $data, Key $key): string { // Validate key. if ($key->getType() !== 'crypto') { @@ -94,7 +94,7 @@ public function encrypt($data, Key $key) * @since 2.0.0 * @throws InvalidKeyException if the key cannot be generated */ - public function generateKey(array $options = []) + public function generateKey(array $options = []): Key { // Generate the encryption key. try { diff --git a/src/Cipher/OpenSSL.php b/src/Cipher/OpenSSL.php index 5aa4f7a0..33be9ad0 100644 --- a/src/Cipher/OpenSSL.php +++ b/src/Cipher/OpenSSL.php @@ -65,7 +65,7 @@ public function __construct(string $iv, string $method) * @throws DecryptionException if the data cannot be decrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function decrypt($data, Key $key) + public function decrypt(string $data, Key $key): string { // Validate key. if ($key->getType() !== 'openssl') { @@ -93,7 +93,7 @@ public function decrypt($data, Key $key) * @throws EncryptionException if the data cannot be encrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function encrypt($data, Key $key) + public function encrypt(string $data, Key $key): string { // Validate key. if ($key->getType() !== 'openssl') { @@ -119,7 +119,7 @@ public function encrypt($data, Key $key) * @since 2.0.0 * @throws InvalidKeyException if the key cannot be generated */ - public function generateKey(array $options = []) + public function generateKey(array $options = []): Key { $passphrase = $options['passphrase'] ?? false; diff --git a/src/Cipher/Sodium.php b/src/Cipher/Sodium.php index bae797f0..bef128b2 100644 --- a/src/Cipher/Sodium.php +++ b/src/Cipher/Sodium.php @@ -45,7 +45,7 @@ class Sodium implements CipherInterface * @throws DecryptionException if the data cannot be decrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function decrypt($data, Key $key) + public function decrypt(string $data, Key $key): string { // Validate key. if ($key->getType() !== 'sodium') { @@ -85,7 +85,7 @@ public function decrypt($data, Key $key) * @throws EncryptionException if the data cannot be encrypted * @throws InvalidKeyTypeException if the key is not valid for the cipher */ - public function encrypt($data, Key $key) + public function encrypt(string $data, Key $key): string { // Validate key. if ($key->getType() !== 'sodium') { @@ -118,7 +118,7 @@ public function encrypt($data, Key $key) * @throws InvalidKeyException if the key cannot be generated * @throws UnsupportedCipherException if the cipher is not supported on the current environment */ - public function generateKey(array $options = []) + public function generateKey(array $options = []): Key { try { // Generate the encryption key. diff --git a/src/CipherInterface.php b/src/CipherInterface.php index 5147ae53..1989cf78 100644 --- a/src/CipherInterface.php +++ b/src/CipherInterface.php @@ -35,7 +35,7 @@ interface CipherInterface * @throws InvalidKeyTypeException if the key is not valid for the cipher * @throws UnsupportedCipherException if the cipher is not supported on the current environment */ - public function decrypt($data, Key $key); + public function decrypt(string $data, Key $key): string; /** * Method to encrypt a data string. @@ -50,7 +50,7 @@ public function decrypt($data, Key $key); * @throws InvalidKeyTypeException if the key is not valid for the cipher * @throws UnsupportedCipherException if the cipher is not supported on the current environment */ - public function encrypt($data, Key $key); + public function encrypt(string $data, Key $key): string; /** * Method to generate a new encryption key[/pair] object. @@ -63,7 +63,7 @@ public function encrypt($data, Key $key); * @throws InvalidKeyException if the key cannot be generated * @throws UnsupportedCipherException if the cipher is not supported on the current environment */ - public function generateKey(array $options = []); + public function generateKey(array $options = []): Key; /** * Check if the cipher is supported in this environment. diff --git a/src/Crypt.php b/src/Crypt.php index 0ffcb134..e71ac2dd 100644 --- a/src/Crypt.php +++ b/src/Crypt.php @@ -69,7 +69,7 @@ public function __construct(?CipherInterface $cipher = null, ?Key $key = null) * @throws InvalidKeyTypeException if the key is not valid for the cipher * @throws UnsupportedCipherException if the cipher is not supported on the current environment */ - public function decrypt($data) + public function decrypt(string $data): string { return $this->cipher->decrypt($data, $this->key); } @@ -86,7 +86,7 @@ public function decrypt($data) * @throws InvalidKeyTypeException if the key is not valid for the cipher * @throws UnsupportedCipherException if the cipher is not supported on the current environment */ - public function encrypt($data) + public function encrypt(string $data): string { return $this->cipher->encrypt($data, $this->key); } @@ -102,7 +102,7 @@ public function encrypt($data) * @throws InvalidKeyException if the key cannot be generated * @throws UnsupportedCipherException if the cipher is not supported on the current environment */ - public function generateKey(array $options = []) + public function generateKey(array $options = []): Key { return $this->cipher->generateKey($options); } @@ -112,11 +112,11 @@ public function generateKey(array $options = []) * * @param Key $key The key object to set. * - * @return Crypt Instance of $this to allow chaining. + * @return static Instance of $this to allow chaining. * * @since 1.0 */ - public function setKey(Key $key) + public function setKey(Key $key): static { $this->key = $key; @@ -132,7 +132,7 @@ public function setKey(Key $key) * * @since 1.0 */ - public static function genRandomBytes($length = 16) + public static function genRandomBytes(int $length = 16): string { return random_bytes($length); }