Skip to content

Commit

Permalink
Add command for generating encryption keys (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto authored Jan 19, 2017
1 parent d962940 commit 5d51e60
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Added support for custom encryption providers.
- Added support for data encryption with [defuse/php-encryption](https://github.com/defuse/php-encryption).
- Added ability to configure which crypto provider should be used. Current available options are `mcrypt` (not recommended since it will be removed in PHP 7.2) and `defuse_php_encryption`.
- Added console command to generate an encryption key for use with `defuse_php_encryption`.

### Removed
- Removed support for PHP 5.3. If you're still using PHP 5.3, please consider upgrading since it reached End Of Life in August 2014. Otherwise, use `1.2.*`.
Expand Down
24 changes: 24 additions & 0 deletions Command/GenerateKeyCommand.php
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());
}
}
5 changes: 1 addition & 4 deletions Resources/doc/setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ You can generate the secret with the following command:

.. code-block :: bash
php -r '
require "vendor/autoload.php";
echo (\Defuse\Crypto\Key::createNewRandomKey())->saveToAsciiSafeString()."\n";
'
bin/console jms_payment_core:generate-key
And then use it in your configuration:

Expand Down
48 changes: 48 additions & 0 deletions Tests/Functional/Command/GenerateKeyCommandTest.php
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();
}
}

0 comments on commit 5d51e60

Please sign in to comment.