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
Showing
2 changed files
with
140 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,76 @@ | ||
<?php | ||
/** | ||
* Ryan's Random Data Library | ||
* | ||
* @package Rych\Random | ||
* @author Ryan Chouinard <rchouinard@gmail.com> | ||
* @copyright Copyright (c) 2013, Ryan Chouinard | ||
* @license MIT License - http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
|
||
namespace Rych\Random\Generator; | ||
|
||
/** | ||
* Cryptographically secure pseudo-random number generator | ||
* | ||
* The cryptographically secure pseudo-random number generator (CSPRNG) API | ||
* provides an easy and reliable way to generate crypto-strong random integers | ||
* and bytes for use within cryptographic contexts. | ||
* | ||
* @package Rych\Random | ||
* @author Ryan Chouinard <rchouinard@gmail.com> | ||
* @copyright Copyright (c) 2013, Ryan Chouinard | ||
* @license MIT License - http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
class CSPRNGenerator implements GeneratorInterface | ||
{ | ||
|
||
/** | ||
* Generate a string of random data. | ||
* | ||
* @param integer $byteCount The desired number of bytes. | ||
* @return string Returns the generated string. | ||
*/ | ||
public function generate($byteCount) | ||
{ | ||
$bytes = ''; | ||
|
||
if (self::isSupported()) { | ||
$mcryptStr = random_bytes($byteCount); | ||
if ($mcryptStr !== false) { | ||
$bytes = $mcryptStr; | ||
} | ||
} | ||
|
||
return str_pad($bytes, $byteCount, chr(0)); | ||
} | ||
|
||
/** | ||
* Test system support for this generator. | ||
* | ||
* @return boolean Returns true if the generator is supported on the current | ||
* platform, otherwise false. | ||
*/ | ||
public static function isSupported() | ||
{ | ||
$supported = false; | ||
if (function_exists('random_bytes') && | ||
version_compare(phpversion(), '7.1', '>=')) { | ||
$supported = true; | ||
} | ||
|
||
return $supported; | ||
} | ||
|
||
/** | ||
* Get the generator priority. | ||
* | ||
* @return integer Returns an integer indicating the priority of the | ||
* generator. Lower numbers represent lower priorities. | ||
*/ | ||
public static function getPriority() | ||
{ | ||
return GeneratorInterface::PRIORITY_HIGH; | ||
} | ||
|
||
} |
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,64 @@ | ||
<?php | ||
/** | ||
* Ryan's Random Data Library | ||
* | ||
* @package Rych\Random | ||
* @author Ryan Chouinard <rchouinard@gmail.com> | ||
* @copyright Copyright (c) 2013, Ryan Chouinard | ||
* @license MIT License - http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
|
||
namespace Rych\Random\Tests\Generator; | ||
|
||
use Rych\Random\Generator\CSPRNGenerator; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
/** | ||
* CSPRN generator tests | ||
* | ||
* @package Rych\Random | ||
* @author Ryan Chouinard <rchouinard@gmail.com> | ||
* @copyright Copyright (c) 2013, Ryan Chouinard | ||
* @license MIT License - http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
class CSPRNGeneratorTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function setUp() | ||
{ | ||
if (!CSPRNGenerator::isSupported()) { | ||
$this->markTestSkipped('CSPRNG is not supported on this platform.'); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
* @return void | ||
*/ | ||
public function testGenerateMethodProducesVaryingResultsEachCall() | ||
{ | ||
$generator = new CSPRNGenerator; | ||
$previous = array (); | ||
for ($i = 0; $i < 10; ++$i) { | ||
$result = $generator->generate(8); | ||
$this->assertTrue(strlen($result) === 8, 'Generator should produce a string length of eight.'); | ||
$this->assertFalse(in_array($result, $previous), 'Generator should not duplicate a previous result in subsequent calls.'); | ||
$previous[] = $result; | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
* @return void | ||
*/ | ||
public function testGetPriorityMethodReturnsInteger() | ||
{ | ||
$this->assertInternalType('integer', CSPRNGenerator::getPriority(), 'Generator should express priority as an integer value.'); | ||
$this->assertTrue(0 < CSPRNGenerator::getPriority() && 4 > CSPRNGenerator::getPriority(), 'Generator should have a priority between 1 and 3.'); | ||
} | ||
|
||
} | ||
|