Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeyer2k committed Oct 2, 2015
1 parent 19eabf7 commit dcdc4b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 28 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"Dcrypt\\": "src/"
}
},
"suggest": {
"paragonie/random_compat": "For better random numbers."
},
"autoload-dev": {
"psr-4": {
"Dcrypt\\Support\\": "helpers/"
Expand Down
29 changes: 6 additions & 23 deletions src/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,39 +46,22 @@ private static function fromMcrypt($bytes)
return $ret;
}

/**
* Get random bytes from Openssl
*
* @param int $bytes Number of bytes to get
*
* @return string
*/
private static function fromOpenssl($bytes)
{
$ret = \openssl_random_pseudo_bytes($bytes, $secure);

if ($secure === false) {
self::toss(); // @codeCoverageIgnore
}

return $ret;
}

/**
* Return securely generated random bytes.
*
* @param int $bytes Number of bytes to get
* @param bool $mcrypt Whether to use mcrypt_create_iv as first choice.
*
* @return string
*/
public static function get($bytes, $mcrypt = true)
public static function get($bytes)
{
if (\function_exists('mcrypt_create_iv') && $mcrypt === true) {
if (function_exists('random_bytes')) {
return random_bytes($bytes);
} elseif (\function_exists('mcrypt_create_iv')) {
return self::fromMcrypt($bytes);
} else {
return self::fromOpenssl($bytes);
}

self::toss();
}

/*
Expand Down
7 changes: 2 additions & 5 deletions tests/RandomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ class RandomTest extends PHPUnit_Framework_TestCase

public function testGet()
{
$len = 2;
$len = 10;

$m = \Dcrypt\Random::get($len, true);
$m = \Dcrypt\Random::get($len);
$this->assertTrue(strlen($m) === $len);

$o = \Dcrypt\Random::get($len, false);
$this->assertTrue(strlen($o) === $len);
}

}

0 comments on commit dcdc4b8

Please sign in to comment.