-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command for generating encryption keys (#208)
- Loading branch information
Showing
4 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace JMS\Payment\CoreBundle\Command; | ||
|
||
use Defuse\Crypto\Key; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class GenerateKeyCommand extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('jms_payment_core:generate-key') | ||
->setDescription('Generate an encryption key') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$output->writeln(Key::createNewRandomKey()->saveToAsciiSafeString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace JMS\Payment\CoreBundle\Tests\Command; | ||
|
||
use JMS\Payment\CoreBundle\Command\GenerateKeyCommand; | ||
use JMS\Payment\CoreBundle\Cryptography\DefusePhpEncryptionService; | ||
use JMS\Payment\CoreBundle\Tests\Functional\BaseTestCase; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
class GenerateKeyCommandTest extends BaseTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
self::createKernel(); | ||
|
||
$application = new Application(self::$kernel); | ||
$application->add(new GenerateKeyCommand()); | ||
|
||
$this->command = $application->find('jms_payment_core:generate-key'); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** | ||
* @runInSeparateProcess | ||
*/ | ||
public function testGeneration() | ||
{ | ||
$key = trim($this->execute(array())); | ||
|
||
$cipher = new DefusePhpEncryptionService($key); | ||
|
||
$this->assertNotEmpty($key); | ||
$this->assertEquals('foo', $cipher->decrypt($cipher->encrypt('foo'))); | ||
} | ||
|
||
private function execute(array $input) | ||
{ | ||
$commandTester = new CommandTester($this->command); | ||
|
||
$commandTester->execute(array_merge(array( | ||
'command' => $this->command->getName(), | ||
), $input)); | ||
|
||
return $commandTester->getDisplay(); | ||
} | ||
} |