Skip to content

Commit 55f14ce

Browse files
committed
Refactor encryption key generation flow to fix booleanNot.alwaysTrue PHPStan error
1 parent 78ca186 commit 55f14ce

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/Command/EncryptionGenerateKeysCommand.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4848
$decryptionKeyExists = $filesystem->exists($this->privateKeyPath);
4949
$encryptionKeyExists = $filesystem->exists($this->publicKeyPath);
5050

51-
if (!$decryptionKeyExists && $encryptionKeyExists) {
52-
$io->error('Unable to generate (private) decryption key because a (public) encryption key exists.');
51+
if ($encryptionKeyExists) {
52+
if (!$decryptionKeyExists) {
53+
// If only the public key exists, generating a new private key is impossible as it would create
54+
// an incompatible key pair, or existing data would become unreadable
55+
$io->error('Unable to generate (private) decryption key because a (public) encryption key exists.');
56+
57+
return Command::FAILURE;
58+
}
5359

54-
return Command::FAILURE;
55-
} elseif ($decryptionKeyExists && $encryptionKeyExists) {
5660
$io->info('Encryption keys already exist.');
57-
} elseif ($decryptionKeyExists && !$encryptionKeyExists) {
61+
} elseif ($decryptionKeyExists) {
62+
// If only the private key exists, we can still regenerate a public key
5863
$decryptionKey = sodium_hex2bin($filesystem->readFile($this->privateKeyPath));
5964
$encryptionKey = sodium_crypto_box_publickey($decryptionKey);
6065

6166
$filesystem->dumpFile($this->publicKeyPath, sodium_bin2hex($encryptionKey));
6267

6368
$io->success('Generated a new (public) encryption key.');
6469
} else {
70+
// Generate a new encryption key pair
6571
$decryptionKey = sodium_crypto_box_keypair();
6672
$encryptionKey = sodium_crypto_box_publickey($decryptionKey);
6773

0 commit comments

Comments
 (0)