Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit - simply move files from phpecc #1

Merged
merged 4 commits into from
Sep 24, 2015
Merged
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tests/output
vendor/

composer.lock
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7
- hhvm

env:
- MATH_LIB=gmp

before_script:
- travis_retry composer selfupdate
- travis_retry composer install --prefer-source

script:
- vendor/bin/phpunit --coverage-clover=coverage.clover

after_success:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

matrix:
fast_finish: true
6 changes: 6 additions & 0 deletions bin/phpecc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env php
<?php

(@include_once __DIR__ . '/../vendor/autoload.php') || @include_once __DIR__ . '/../../../autoload.php';

(new \Mdanter\Ecc\Console\Application())->run();
46 changes: 46 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "mdanter/console",
"description": "PHPECC console application",
"type": "application",
"homepage": "https://github.com/phpecc/console",
"time": "2015-09-23 12:00:00",
"license": "MIT",
"authors": [
{
"name": "Matyas Danter",
"homepage": "http://matejdanter.com/",
"role": "Author"
},
{
"name": "Thibaud Fabre",
"email": "thibaud@aztech.io",
"homepage": "http://aztech.io",
"role": "Maintainer"
},
{
"name": "Drak",
"email": "drak@zikula.org",
"homepage": "http://zikula.org",
"role": "Maintainer"
}
],
"require": {
"symfony/console": "~2.6",
"mdanter/ecc": "~0.3"
},
"require-dev": {
"phpunit/phpunit": "~4.1",
"squizlabs/php_codesniffer": "~2",
"symfony/yaml": "~2.6"
},
"autoload": {
"psr-4": {
"Mdanter\\Ecc\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mdanter\\Ecc\\Tests\\": "tests/unit"
}
}
}
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true"
convertErrorsToExceptions="true" convertNoticesToExceptions="true"
convertWarningsToExceptions="true" processIsolation="false"
stopOnFailure="false">
<php>
<const name="PHPUNIT_DEBUG" value="false" />
<ini name="display_errors" value="On" />
<ini name="error_reporting" value="32767" />
</php>
<testsuites>
<testsuite name="All">
<directory suffix="Test.php">./tests/unit</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="./tests/output/Coverage/"
charset="UTF-8" yui="true" highlight="true" />
<log type="junit" target="./tests/output/Results/Results.xml"
logIncompleteSkipped="true" />
</logging>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
36 changes: 36 additions & 0 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Mdanter\Ecc\Console;

use Symfony\Component\Console\Application as ConsoleApplication;
use Mdanter\Ecc\Console\Commands\GenerateKeyPairCommand;
use Mdanter\Ecc\Console\Commands\HexDecCommand;
use Mdanter\Ecc\Console\Commands\DecHexCommand;
use Mdanter\Ecc\Console\Commands\ParsePublicKeyCommand;
use Mdanter\Ecc\Console\Commands\ParsePrivateKeyCommand;
use Mdanter\Ecc\Console\Commands\GeneratePublicKeyCommand;
use Mdanter\Ecc\Console\Commands\ListCurvesCommand;
use Mdanter\Ecc\Console\Commands\DumpAsnCommand;

class Application extends ConsoleApplication
{

/**
* @return array|\Symfony\Component\Console\Command\Command[]
*/
protected function getDefaultCommands()
{
$commands = parent::getDefaultCommands();

$commands[] = new DumpAsnCommand();
$commands[] = new GenerateKeyPairCommand();
$commands[] = new GeneratePublicKeyCommand();
$commands[] = new ListCurvesCommand();
$commands[] = new ParsePrivateKeyCommand();
$commands[] = new ParsePublicKeyCommand();
$commands[] = new HexDecCommand();
$commands[] = new DecHexCommand();

return $commands;
}
}
106 changes: 106 additions & 0 deletions src/Console/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Mdanter\Ecc\Console\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Mdanter\Ecc\File\PemLoader;
use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer;
use Mdanter\Ecc\Serializer\PrivateKey\PemPrivateKeySerializer;
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer;
use Mdanter\Ecc\Serializer\PublicKey\PemPublicKeySerializer;
use Mdanter\Ecc\File\DerFileLoader;
use Mdanter\Ecc\File\FileLoader;

abstract class AbstractCommand extends Command
{
/**
* @param InputInterface $input
* @param FileLoader $loader
* @param $fileOptionName
* @param $dataOptionName
* @return mixed
*/
protected function getPrivateKeyData(InputInterface $input, FileLoader $loader, $fileOptionName, $dataOptionName)
{
if ($infile = $input->getOption($fileOptionName)) {
if (! file_exists($infile)) {
$infile = getcwd().'/'.$infile;
}

$data = $loader->loadPrivateKeyData(realpath($infile));
} else {
$data = $input->getArgument($dataOptionName);
}

return $data;
}

/**
* @param InputInterface $input
* @param FileLoader $loader
* @param $fileOptionName
* @param $dataOptionName
* @return mixed
*/
protected function getPublicKeyData(InputInterface $input, FileLoader $loader, $fileOptionName, $dataOptionName)
{
if ($infile = $input->getOption($fileOptionName)) {
if (! file_exists($infile)) {
$infile = getcwd().'/'.$infile;
}

$data = $loader->loadPublicKeyData(realpath($infile));
} else {
$data = $input->getArgument($dataOptionName);
}

return $data;
}

/**
* @param InputInterface $input
* @param $formatOptionName
* @return DerFileLoader|PemLoader
*/
protected function getLoader(InputInterface $input, $formatOptionName)
{
if ($input->getOption($formatOptionName) == 'der') {
return new DerFileLoader();
}

return new PemLoader();
}

/**
* @param InputInterface $input
* @param $formatOptionName
* @return DerPrivateKeySerializer|PemPrivateKeySerializer
*/
protected function getPrivateKeySerializer(InputInterface $input, $formatOptionName)
{
$serializer = new DerPrivateKeySerializer();

if ($input->getOption($formatOptionName) == 'pem') {
$serializer = new PemPrivateKeySerializer($serializer);
}

return $serializer;
}

/**
* @param InputInterface $input
* @param $formatOptionName
* @return DerPublicKeySerializer|PemPublicKeySerializer
*/
protected function getPublicKeySerializer(InputInterface $input, $formatOptionName)
{
$serializer = new DerPublicKeySerializer();

if ($input->getOption($formatOptionName) == 'pem') {
$serializer = new PemPublicKeySerializer($serializer);
}

return $serializer;
}
}
34 changes: 34 additions & 0 deletions src/Console/Commands/DecHexCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Mdanter\Ecc\Console\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Mdanter\Ecc\Math\MathAdapterFactory;

class DecHexCommand extends Command
{
/**
* @return void
*/
protected function configure()
{
$this->setName('dechex')
->addArgument('dec', InputArgument::REQUIRED, 'Decimal value');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$hex = $input->getArgument('dec');
$adapter = MathAdapterFactory::getAdapter();

$output->writeln($adapter->decHex($hex));
}
}
68 changes: 68 additions & 0 deletions src/Console/Commands/DumpAsnCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Mdanter\Ecc\Console\Commands;

use FG\ASN1\Identifier;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use FG\ASN1\Object;

class DumpAsnCommand extends AbstractCommand
{

/**
* @return void
*/
protected function configure()
{
$this->setName('dump-asn')
->setDescription('Dumps the ASN.1 object structure.')
->addArgument('data', InputArgument::OPTIONAL)
->addOption('infile', null, InputOption::VALUE_OPTIONAL)
->addOption('in', null, InputOption::VALUE_OPTIONAL, 'Input format (der or pem). Defaults to pem.', 'pem');
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @throws \FG\ASN1\Exception\ParserException
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$loader = $this->getLoader($input, 'in');
$data = $this->getPrivateKeyData($input, $loader, 'infile', 'data');

$asnObject = Object::fromBinary(base64_decode($data));
$this->printObject($output, $asnObject);
}

/**
* @param OutputInterface $output
* @param Object $object
* @param int $depth
* @return void
* @throws \FG\ASN1\Exception\NotImplementedException
*/
private function printObject(OutputInterface $output, Object $object, $depth = 0)
{
$treeSymbol = '';
$depthString = str_repeat('─', $depth);
if ($depth > 0) {
$treeSymbol = '├';
}

$name = Identifier::getShortName($object->getType());
$output->write("{$treeSymbol}{$depthString}<comment>{$name}</comment> : ");
$output->writeln($object->__toString());

$content = $object->getContent();
if (is_array($content)) {
foreach ($object as $child) {
$this->printObject($output, $child, $depth+1);
}
}
}
}
Loading