diff --git a/composer.json b/composer.json index aadce7c6..0ed9c7ff 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,9 @@ "Dcrypt\\": "src/" } }, + "suggest": { + "paragonie/random_compat": "For better random numbers." + }, "autoload-dev": { "psr-4": { "Dcrypt\\Support\\": "helpers/" diff --git a/src/Random.php b/src/Random.php index 5223f0b2..84c6437b 100644 --- a/src/Random.php +++ b/src/Random.php @@ -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(); } /* diff --git a/tests/RandomTest.php b/tests/RandomTest.php index c8922587..55857b6f 100644 --- a/tests/RandomTest.php +++ b/tests/RandomTest.php @@ -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); } }