-
Notifications
You must be signed in to change notification settings - Fork 25
Removed the OpenSSL usage #16
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,8 @@ abstract class Rand | |
protected static $generator = null; | ||
|
||
/** | ||
* Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback | ||
* Generate random bytes using different approaches | ||
* If PHP 7 is running we use the random_bytes() function | ||
* | ||
* @param int $length | ||
* @param bool $strong true if you need a strong random generator (cryptography) | ||
|
@@ -39,11 +40,8 @@ public static function getBytes($length, $strong = false) | |
return false; | ||
} | ||
|
||
if (function_exists('openssl_random_pseudo_bytes')) { | ||
$bytes = openssl_random_pseudo_bytes($length, $usable); | ||
if (true === $usable) { | ||
return $bytes; | ||
} | ||
if (function_exists('random_bytes')) { // available in PHP 7 | ||
return random_bytes($length); | ||
} | ||
if (function_exists('mcrypt_create_iv')) { | ||
$bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); | ||
|
@@ -56,7 +54,7 @@ public static function getBytes($length, $strong = false) | |
if (true === $strong && false === $checkAlternatives) { | ||
throw new Exception\RuntimeException( | ||
'This PHP environment doesn\'t support secure random number generation. ' . | ||
'Please consider installing the OpenSSL and/or Mcrypt extensions' | ||
'Please consider installing Mcrypt extension or use PHP 7' | ||
); | ||
} | ||
$generator = self::getAlternativeGenerator(); | ||
|
@@ -76,8 +74,7 @@ public static function getAlternativeGenerator() | |
if (!class_exists('RandomLib\\Factory')) { | ||
throw new Exception\RuntimeException( | ||
'The RandomLib fallback pseudorandom number generator (PRNG) ' | ||
. ' must be installed in the absence of the OpenSSL and ' | ||
. 'Mcrypt extensions' | ||
. ' must be installed in the absence of a secure source' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit cryptic; as an end-user, if I don't know cryptography, I might not understand what "absence of a secure source" refers to. Is it cryptic on purpose? i.e., to prevent an attacker from understanding the capabilities of the server if the exception is displayed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @weierophinney this is not cryptic by purpose, but definitely you find out a good point here. We can rephrase with "The RandomLib fallback pseudorandom generator is not installed. Please install it to support secure random numbers". What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect. |
||
); | ||
} | ||
$factory = new RandomLib\Factory; | ||
|
@@ -117,6 +114,9 @@ public static function getInteger($min, $max, $strong = false) | |
'The min parameter must be lower than max parameter' | ||
); | ||
} | ||
if (function_exists('random_int')) { // available in PHP 7 | ||
return random_int($min, $max); | ||
} | ||
$range = $max - $min; | ||
if ($range == 0) { | ||
return $max; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rephrase this to "Please consider either installing ext/mcrypt or upgrading to PHP 7".