From 047924657b32175f6f3d9db5f0facc1e24e55e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Unger?= Date: Wed, 9 Dec 2015 14:25:03 +0100 Subject: [PATCH] Initial commit --- .coveralls.yml | 4 ++ .gitattributes | 6 +++ .gitignore | 4 ++ .travis.yml | 25 ++++++++++++ LICENSE | 22 ++++++++++ README.md | 40 +++++++++++++++++++ composer.json | 28 +++++++++++++ phpunit.xml.dist | 16 ++++++++ src/DI/EntropyExtension.php | 21 ++++++++++ src/Exception/CsprngException.php | 13 ++++++ src/Exception/IException.php | 11 +++++ src/Exception/McryptException.php | 13 ++++++ src/Exception/OpenSslException.php | 13 ++++++ src/Provider/IEntropyProvider.php | 17 ++++++++ src/Provider/OpenSslProvider.php | 27 +++++++++++++ tests/Entropy/DI/EntropyExtensionTest.php | 39 ++++++++++++++++++ .../Entropy/Provider/OpenSslProviderTest.php | 20 ++++++++++ tests/bootstrap.php | 23 +++++++++++ 18 files changed, 342 insertions(+) create mode 100644 .coveralls.yml create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/DI/EntropyExtension.php create mode 100644 src/Exception/CsprngException.php create mode 100644 src/Exception/IException.php create mode 100644 src/Exception/McryptException.php create mode 100644 src/Exception/OpenSslException.php create mode 100644 src/Provider/IEntropyProvider.php create mode 100644 src/Provider/OpenSslProvider.php create mode 100644 tests/Entropy/DI/EntropyExtensionTest.php create mode 100644 tests/Entropy/Provider/OpenSslProviderTest.php create mode 100644 tests/bootstrap.php diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..2dc2afd --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,4 @@ +service_name: travis-ci +src_dir: src +coverage_clover: coverage.xml +json_path: coverage.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..75456da --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +tests export-ignore +.coveralls.yml export-ignore +.gitattributes export-ignore +.gitignore export-ignore +.travis.yml export-ignore +phpunit.xml.dist export-ignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92379ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +tests/tmp/* +vendor/* +composer.lock +coverage.xml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..faefe44 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,25 @@ +language: php + +sudo: false + +php: + - 5.5 + - 5.6 + - 7.0 + - hhvm + +matrix: + allow_failures: + - php: hhvm + +before_install: + - composer self-update + +install: + - composer install --no-interaction --prefer-source + +script: + - ./vendor/bin/phpunit + +after_script: + - ./vendor/bin/coveralls diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fb3a715 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Lukáš Unger + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..db9b8dd --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# lookyman/Entropy + +[![Build Status](https://travis-ci.org/lookyman/entropy.svg?branch=master)](https://travis-ci.org/lookyman/entropy) +[![Downloads](https://img.shields.io/packagist/dt/lookyman/entropy.svg)](https://packagist.org/packages/lookyman/entropy) +[![Latest stable](https://img.shields.io/packagist/v/lookyman/entropy.svg)](https://packagist.org/packages/lookyman/entropy) +[![Coverage Status](https://coveralls.io/repos/lookyman/entropy/badge.svg?branch=master&service=github)](https://coveralls.io/github/lookyman/entropy?branch=master) + +Requirements +------ + +lookyman/Entropy requires PHP 5.5 or higher. + +- [Nette DI](https://github.com/nette/di) + + +Installation +------ + +```sh +$ composer require lookyman/entropy +``` + +You can enable the extension using your neon config: + +```yml +extensions: + entropy: lookyman\Entropy\DI\EntropyExtension +``` + + +Usage +------ + +This extension provides you with the service `lookyman\Entropy\Provider\IEntropyProvider`, that you can inject into your application. + +```php +// $this->provider in an instance of lookyman\Entropy\Provider\IEntropyProvider + +var_dump($this->provider->getRandomPseudoBytes(8)); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1aaf9d0 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "lookyman/entropy", + "description": "Entropy provider library for PHP", + "keywords": ["entropy", "Nette"], + "license": ["MIT"], + "authors": [ + { + "name": "Lukáš Unger", + "email": "looky.msc@gmail.com", + "homepage": "https://lookyman.net" + } + ], + "require": { + "php": ">=5.5", + "ext-openssl": "*", + "nette/di": "^2.3" + }, + "require-dev": { + "nette/robot-loader": "^2.3", + "phpunit/phpunit": "^4.8", + "satooshi/php-coveralls": "dev-master" + }, + "autoload": { + "psr-4": { + "lookyman\\Entropy\\": "src/" + } + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..87c99bf --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,16 @@ + + + + + ./tests + + + + + ./src + + + + + + diff --git a/src/DI/EntropyExtension.php b/src/DI/EntropyExtension.php new file mode 100644 index 0000000..7f07ce3 --- /dev/null +++ b/src/DI/EntropyExtension.php @@ -0,0 +1,21 @@ +getContainerBuilder(); + + $builder->addDefinition($this->prefix('provider')) + ->setClass(IEntropyProvider::class) + ->setFactory(OpenSslProvider::class); + } + +} diff --git a/src/Exception/CsprngException.php b/src/Exception/CsprngException.php new file mode 100644 index 0000000..4cbda3d --- /dev/null +++ b/src/Exception/CsprngException.php @@ -0,0 +1,13 @@ + + */ +class CsprngException extends \RuntimeException implements IException +{ + + const BAD_RANDOM = 1; + +} diff --git a/src/Exception/IException.php b/src/Exception/IException.php new file mode 100644 index 0000000..82dd3bc --- /dev/null +++ b/src/Exception/IException.php @@ -0,0 +1,11 @@ + + */ +interface IException +{ + +} diff --git a/src/Exception/McryptException.php b/src/Exception/McryptException.php new file mode 100644 index 0000000..0114ec2 --- /dev/null +++ b/src/Exception/McryptException.php @@ -0,0 +1,13 @@ + + */ +class McryptException extends \RuntimeException implements IException +{ + + const BAD_RANDOM = 1; + +} diff --git a/src/Exception/OpenSslException.php b/src/Exception/OpenSslException.php new file mode 100644 index 0000000..b7e3ca9 --- /dev/null +++ b/src/Exception/OpenSslException.php @@ -0,0 +1,13 @@ + + */ +class OpenSslException extends \RuntimeException implements IException +{ + + const BAD_RANDOM = 1; + +} diff --git a/src/Provider/IEntropyProvider.php b/src/Provider/IEntropyProvider.php new file mode 100644 index 0000000..e48c25f --- /dev/null +++ b/src/Provider/IEntropyProvider.php @@ -0,0 +1,17 @@ + + */ +interface IEntropyProvider +{ + + /** + * @param int $length + * @return string + */ + function getRandomPseudoBytes($length); + +} diff --git a/src/Provider/OpenSslProvider.php b/src/Provider/OpenSslProvider.php new file mode 100644 index 0000000..b452032 --- /dev/null +++ b/src/Provider/OpenSslProvider.php @@ -0,0 +1,27 @@ + + */ +class OpenSslProvider implements IEntropyProvider +{ + + /** + * @param int $length + * @return string + * @throws OpenSslException + */ + public function getRandomPseudoBytes($length) + { + $data = openssl_random_pseudo_bytes((int) $length, $strong); + if ($data === FALSE || $strong !== TRUE) { + throw new OpenSslException('Could not get enough cryptographically strong bytes of entropy.', OpenSslException::BAD_RANDOM); + } + return $data; + } + +} diff --git a/tests/Entropy/DI/EntropyExtensionTest.php b/tests/Entropy/DI/EntropyExtensionTest.php new file mode 100644 index 0000000..54487a4 --- /dev/null +++ b/tests/Entropy/DI/EntropyExtensionTest.php @@ -0,0 +1,39 @@ +createContainer(); + + $this->assertInstanceOf(OpenSslProvider::class, $service1 = $container->getService('entropy.provider')); + $this->assertInstanceOf(OpenSslProvider::class, $service2 = $container->getByType(IEntropyProvider::class)); + $this->assertSame($service1, $service2); + } + + /** + * @param array $config + * @return \Nette\DI\Container + */ + private function createContainer(array $config = []) + { + $loader = new ContainerLoader(TEMP_DIR, TRUE); + $class = $loader->load($config, function (Compiler $compiler) use ($config) { + $compiler->addExtension('entropy', new EntropyExtension); + $compiler->addConfig($config); + }); + $container = new $class; + $container->initialize(); + return $container; + } + +} diff --git a/tests/Entropy/Provider/OpenSslProviderTest.php b/tests/Entropy/Provider/OpenSslProviderTest.php new file mode 100644 index 0000000..440f038 --- /dev/null +++ b/tests/Entropy/Provider/OpenSslProviderTest.php @@ -0,0 +1,20 @@ + + */ +class OpenSslProviderTest extends \PHPUnit_Framework_TestCase +{ + + public function testGetRandomPseudoBytes() + { + $data = (new OpenSslProvider)->getRandomPseudoBytes(16); + $this->assertInternalType('string', $data); + $this->assertSame(16, strlen($data)); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..13e1d64 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,23 @@ +autoRebuild = TRUE; +$loader->setCacheStorage(new DevNullStorage) + ->addDirectory(__DIR__) + ->register(); + +define('TEMP_DIR', __DIR__ . '/tmp'); + +call_user_func(function ($dir) { + if (!is_dir($dir)) { + mkdir($dir); + } + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST) as $entry) { + $entry->isDir() ? rmdir($entry) : unlink($entry); + } +}, TEMP_DIR);