This repository has been archived by the owner on May 4, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0479246
Showing
18 changed files
with
342 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
service_name: travis-ci | ||
src_dir: src | ||
coverage_clover: coverage.xml | ||
json_path: coverage.json |
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,6 @@ | ||
tests export-ignore | ||
.coveralls.yml export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.travis.yml export-ignore | ||
phpunit.xml.dist export-ignore |
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,4 @@ | ||
tests/tmp/* | ||
vendor/* | ||
composer.lock | ||
coverage.xml |
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,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 |
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,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. | ||
|
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,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)); | ||
``` |
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,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/" | ||
} | ||
} | ||
} |
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,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="./tests/bootstrap.php" verbose="true"> | ||
<testsuites> | ||
<testsuite name="lookyman/Entropy"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory>./src</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-clover" target="./coverage.xml"/> | ||
</logging> | ||
</phpunit> |
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,21 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\DI; | ||
|
||
use Nette\DI\CompilerExtension; | ||
use lookyman\Entropy\Provider\IEntropyProvider; | ||
use lookyman\Entropy\Provider\OpenSslProvider; | ||
|
||
class EntropyExtension extends CompilerExtension | ||
{ | ||
|
||
public function loadConfiguration() | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
$builder->addDefinition($this->prefix('provider')) | ||
->setClass(IEntropyProvider::class) | ||
->setFactory(OpenSslProvider::class); | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Exception; | ||
|
||
/** | ||
* @author Lukáš Unger <looky.msc@gmail.com> | ||
*/ | ||
class CsprngException extends \RuntimeException implements IException | ||
{ | ||
|
||
const BAD_RANDOM = 1; | ||
|
||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Exception; | ||
|
||
/** | ||
* @author Lukáš Unger <looky.msc@gmail.com> | ||
*/ | ||
interface IException | ||
{ | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Exception; | ||
|
||
/** | ||
* @author Lukáš Unger <looky.msc@gmail.com> | ||
*/ | ||
class McryptException extends \RuntimeException implements IException | ||
{ | ||
|
||
const BAD_RANDOM = 1; | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Exception; | ||
|
||
/** | ||
* @author Lukáš Unger <looky.msc@gmail.com> | ||
*/ | ||
class OpenSslException extends \RuntimeException implements IException | ||
{ | ||
|
||
const BAD_RANDOM = 1; | ||
|
||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Provider; | ||
|
||
/** | ||
* @author Lukáš Unger <looky.msc@gmail.com> | ||
*/ | ||
interface IEntropyProvider | ||
{ | ||
|
||
/** | ||
* @param int $length | ||
* @return string | ||
*/ | ||
function getRandomPseudoBytes($length); | ||
|
||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Provider; | ||
|
||
use lookyman\Entropy\Exception\OpenSslException; | ||
|
||
/** | ||
* @author Lukáš Unger <looky.msc@gmail.com> | ||
*/ | ||
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; | ||
} | ||
|
||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Tests\DI; | ||
|
||
use Nette\DI\Compiler; | ||
use Nette\DI\ContainerLoader; | ||
use lookyman\Entropy\DI\EntropyExtension; | ||
use lookyman\Entropy\Provider\IEntropyProvider; | ||
use lookyman\Entropy\Provider\OpenSslProvider; | ||
|
||
class EntropyExtensionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testServices() | ||
{ | ||
$container = $this->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; | ||
} | ||
|
||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace lookyman\Entropy\Tests\Provider; | ||
|
||
use lookyman\Entropy\Provider\OpenSslProvider; | ||
|
||
/** | ||
* @author Lukáš Unger <looky.msc@gmail.com> | ||
*/ | ||
class OpenSslProviderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function testGetRandomPseudoBytes() | ||
{ | ||
$data = (new OpenSslProvider)->getRandomPseudoBytes(16); | ||
$this->assertInternalType('string', $data); | ||
$this->assertSame(16, strlen($data)); | ||
} | ||
|
||
} |
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,23 @@ | ||
<?php | ||
|
||
use Nette\Caching\Storages\DevNullStorage; | ||
use Nette\Loaders\RobotLoader; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$loader = new RobotLoader; | ||
$loader->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); |