|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodedMonkey\Dirigent\Command; |
| 4 | + |
| 5 | +use CodedMonkey\Dirigent\Encryption\Encryption; |
| 6 | +use CodedMonkey\Dirigent\Encryption\EncryptionException; |
| 7 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\Console\Style\StyleInterface; |
| 12 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 13 | +use Symfony\Component\Filesystem\Filesystem; |
| 14 | + |
| 15 | +#[AsCommand( |
| 16 | + name: 'encryption:generate-keys', |
| 17 | + description: 'Generates an encryption key pair', |
| 18 | +)] |
| 19 | +class EncryptionGenerateKeysCommand extends Command |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + public readonly ?string $privateKeyPath, |
| 23 | + public readonly ?string $publicKeyPath, |
| 24 | + ) { |
| 25 | + parent::__construct(); |
| 26 | + } |
| 27 | + |
| 28 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 29 | + { |
| 30 | + $io = new SymfonyStyle($input, $output); |
| 31 | + |
| 32 | + if (!$this->privateKeyPath || !$this->publicKeyPath) { |
| 33 | + $io->info('Encryption key files are disabled.'); |
| 34 | + |
| 35 | + return Command::SUCCESS; |
| 36 | + } |
| 37 | + |
| 38 | + $filesystem = new Filesystem(); |
| 39 | + |
| 40 | + $decryptionKeyExists = $filesystem->exists($this->privateKeyPath); |
| 41 | + $encryptionKeyExists = $filesystem->exists($this->publicKeyPath); |
| 42 | + |
| 43 | + if (!$decryptionKeyExists && $encryptionKeyExists) { |
| 44 | + $io->error('Unable to generate (private) decryption key because a (public) encryption key exists.'); |
| 45 | + |
| 46 | + return Command::FAILURE; |
| 47 | + } elseif ($decryptionKeyExists && $encryptionKeyExists) { |
| 48 | + $io->info('Encryption keys already exist.'); |
| 49 | + } elseif ($decryptionKeyExists && !$encryptionKeyExists) { |
| 50 | + $decryptionKey = sodium_hex2bin($filesystem->readFile($this->privateKeyPath)); |
| 51 | + $encryptionKey = sodium_crypto_box_publickey($decryptionKey); |
| 52 | + |
| 53 | + $filesystem->dumpFile($this->publicKeyPath, sodium_bin2hex($encryptionKey)); |
| 54 | + |
| 55 | + $io->success('Generated a new (public) encryption key.'); |
| 56 | + } else { |
| 57 | + $decryptionKey = sodium_crypto_box_keypair(); |
| 58 | + $encryptionKey = sodium_crypto_box_publickey($decryptionKey); |
| 59 | + |
| 60 | + $filesystem->dumpFile($this->privateKeyPath, sodium_bin2hex($decryptionKey)); |
| 61 | + $filesystem->dumpFile($this->publicKeyPath, sodium_bin2hex($encryptionKey)); |
| 62 | + |
| 63 | + $io->success('Generated encryption keys.'); |
| 64 | + } |
| 65 | + |
| 66 | + return $this->validateKeys($io); |
| 67 | + } |
| 68 | + |
| 69 | + private function validateKeys(StyleInterface $output): int |
| 70 | + { |
| 71 | + $encryption = Encryption::createFromFiles($this->privateKeyPath, $this->publicKeyPath, []); |
| 72 | + |
| 73 | + $value = 'thank you for the music'; |
| 74 | + |
| 75 | + try { |
| 76 | + $sealedValue = $encryption->seal($value); |
| 77 | + $revealedValue = $encryption->reveal($sealedValue); |
| 78 | + |
| 79 | + if ($value !== $revealedValue) { |
| 80 | + throw new EncryptionException('Encryption failed'); |
| 81 | + } |
| 82 | + |
| 83 | + return Command::SUCCESS; |
| 84 | + } catch (EncryptionException $exception) { |
| 85 | + $output->error('Encryption key validation failed: ' . $exception->getMessage()); |
| 86 | + |
| 87 | + return Command::FAILURE; |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments