Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Tests/NativeTypesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Crypt\Tests;

use Joomla\Crypt\Cipher\Crypto;
use Joomla\Crypt\Cipher\OpenSSL;
use Joomla\Crypt\Cipher\Sodium;
use Joomla\Crypt\CipherInterface;
use Joomla\Crypt\Crypt;
use Joomla\Crypt\Key;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* Checks the native types of the public API.
*/
class NativeTypesTest extends TestCase
{
/**
* Methods and their expected return types
*
* @return array
*/
public static function dataReturnTypes(): array
{
$cases = [
'Crypt::decrypt()' => [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');
}
}
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
43 changes: 43 additions & 0 deletions docs/v4-to-v5-update.md
Original file line number Diff line number Diff line change
@@ -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.

<!-- Section: Crypt requires a cipher and a key -->


## 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.


<!-- Section: Sodium::setNonce() removed -->


## Dependency changes

| Package | v4 (4.0.0) | v5 (5.0.0) |
|---|---|---|
| `php` | `^8.3.0` | `^8.3.0` |
6 changes: 3 additions & 3 deletions src/Cipher/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/Cipher/OpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand All @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/Cipher/Sodium.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/CipherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions src/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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;

Expand All @@ -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);
}
Expand Down
Loading