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
49 changes: 49 additions & 0 deletions Tests/CryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

namespace Joomla\Crypt\Tests;

use Joomla\Crypt\Cipher\Crypto;
use Joomla\Crypt\CipherInterface;
use Joomla\Crypt\Crypt;
use Joomla\Crypt\Key;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -144,4 +147,50 @@ public function testGenRandomBytes($length)
\strlen(Crypt::genRandomBytes($length))
);
}

/**
* Constructor arguments that rely on the deprecated defaults
*
* @return array
*/
public static function dataDeprecatedConstructorArguments(): array
{
return [
'no arguments' => [false, false],
'no key' => [true, false],
'no cipher' => [false, true],
];
}

/**
* @testdox Omitting the cipher or the key triggers a deprecation
*
* @param boolean $withCipher Whether to pass a cipher
* @param boolean $withKey Whether to pass a key
*/
#[AllowMockObjectsWithoutExpectations]
#[DataProvider('dataDeprecatedConstructorArguments')]
public function testConstructorWithoutCipherOrKeyTriggersDeprecation(bool $withCipher, bool $withKey)
{
$cipher = new Crypto();

$this->expectUserDeprecationMessage(
'Since joomla/crypt __DEPLOY_VERSION__: Calling Joomla\Crypt\Crypt::__construct() without a cipher and a key is deprecated.'
. ' Both arguments will be required in 5.0.'
);

new Crypt($withCipher ? $cipher : null, $withKey ? $cipher->generateKey() : null);
}

/**
* @testdox The deprecated defaults still use the Crypto cipher with a generated key
*/
#[AllowMockObjectsWithoutExpectations]
#[IgnoreDeprecations]
public function testConstructorDefaultsStillWork()
{
$crypt = new Crypt();

$this->assertSame('data', $crypt->decrypt($crypt->encrypt('data')));
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"homepage": "https://github.com/joomla-framework/crypt",
"license": "GPL-2.0-or-later",
"require": {
"php": "^8.3.0"
"php": "^8.3.0",
"symfony/deprecation-contracts": "^2|^3"
},
"require-dev": {
"defuse/php-encryption": "^2.0",
Expand Down
5 changes: 5 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ $ciphertext = $crypt->encrypt('some secret value');
$plaintext = $crypt->decrypt($ciphertext);
```

Always pass both the cipher and the key to `Crypt`. Omitting either is deprecated since 4.1 and
triggers an `E_USER_DEPRECATED` notice; both will be required in 5.0. Without a cipher, `Crypt`
falls back to `Crypto`; without a key, it generates a new one that is never stored, so data
encrypted with it cannot be decrypted by any other instance.

To use a stored key later, rebuild the `Key` object with the same type:

```php
Expand Down
13 changes: 11 additions & 2 deletions src/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,22 @@ class Crypt
* Object Constructor takes an optional key to be used for encryption/decryption. If no key is given then the
* secret word from the configuration object is used.
*
* @param ?CipherInterface $cipher The encryption cipher object.
* @param ?Key $key The encryption key[/pair)].
* @param ?CipherInterface $cipher The encryption cipher object. Omitting it is deprecated.
* @param ?Key $key The encryption key[/pair)]. Omitting it is deprecated.
*
* @since 1.0
*/
public function __construct(?CipherInterface $cipher = null, ?Key $key = null)
{
if ($cipher === null || $key === null) {
trigger_deprecation(
'joomla/crypt',
'__DEPLOY_VERSION__',
'Calling %s() without a cipher and a key is deprecated. Both arguments will be required in 5.0.',
__METHOD__
);
}

// Set the encryption cipher.
$this->cipher = $cipher ?: new Crypto();

Expand Down
Loading