Skip to content
Closed
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
16 changes: 16 additions & 0 deletions Tests/Cipher/OpenSSLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Joomla\Crypt\Tests\Cipher;

use Joomla\Crypt\Cipher\OpenSSL;
use Joomla\Crypt\Exception\InvalidKeyException;
use Joomla\Crypt\Key;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -74,6 +75,21 @@ public function testDataEncryptionAndDecryption($data)
$this->assertSame($data, $decrypted);
}

/**
* @testdox Generating a key without a passphrase reports the missing option
*/
public function testGenerateKeyRequiresAPassphrase()
{
$cipher = new OpenSSL('1234567890123456', 'aes-128-cbc');

$this->expectException(InvalidKeyException::class);
$this->expectExceptionMessage(
"The 'passphrase' option is missing. Its value is used as the key itself, not as a file path."
);

$cipher->generateKey();
}

/**
* @testdox Validates keys are correctly generated
*
Expand Down
6 changes: 3 additions & 3 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ $stored = base64_encode($nonce) . ':' . base64_encode($cipher->encrypt($message,
> stream reuse; the missing authentication means a modified ciphertext decrypts without complaint.
> Use `Cipher\Crypto` instead.

If you are stuck with it, one more detail matters. The option is called `passphrase` and the docs
describe it as a passphrase file, but the value is never read as a file — it is handed to
`openssl_encrypt()` as the key material verbatim:
If you are stuck with it, one more detail matters. The option is called `passphrase`, and before 4.1 the
error for a missing one called it a "passphrase file", but the value is never read as a file — it is
handed to `openssl_encrypt()` as the key material verbatim:

```php
$key = $cipher->generateKey(['passphrase' => '/path/to/secret.dat']);
Expand Down
2 changes: 1 addition & 1 deletion src/Cipher/OpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function generateKey(array $options = [])
$passphrase = $options['passphrase'] ?? false;

if ($passphrase === false) {
throw new InvalidKeyException('Missing passphrase file');
throw new InvalidKeyException("The 'passphrase' option is missing. Its value is used as the key itself, not as a file path.");
}

return new Key('openssl', $passphrase, 'unused');
Expand Down
Loading