Add RandomBytesGenerator for use with PHP 7.

This commit is contained in:
Ben Ramsey
2015-07-22 21:34:15 +00:00
parent a06a01b07d
commit 1bdc10a306
3 changed files with 47 additions and 0 deletions
+22
View File
@@ -24,6 +24,14 @@ class RandomGeneratorFactory
*/
public static $forceNoOpensslRandomPseudoBytes = false;
/**
* For testing, random_bytes() override; if true, treat as if random_bytes()
* is not available.
*
* @var bool
*/
public static $forceNoRandomBytes = false;
/**
* Returns true if the system has openssl_random_pseudo_bytes()
*
@@ -34,8 +42,22 @@ class RandomGeneratorFactory
return (function_exists('openssl_random_pseudo_bytes') && !self::$forceNoOpensslRandomPseudoBytes);
}
/**
* Returns true if the system has random_bytes()
*
* @return bool
*/
protected static function hasRandomBytes()
{
return (function_exists('random_bytes') && !self::$forceNoRandomBytes);
}
public static function getGenerator()
{
if (self::hasRandomBytes()) {
return new RandomBytesGenerator();
}
if (self::hasOpensslRandomPseudoBytes()) {
return new OpenSslGenerator();
}