Skip to content
Snippets Groups Projects
Commit 6989e51c authored by Andreas Åkre Solberg's avatar Andreas Åkre Solberg
Browse files

Added a fallback solution if having troubles with dev/urandom. Discovered a...

Added a fallback solution if having troubles with dev/urandom. Discovered a case where RHEL4 returned one more byte than requested...

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@851 44740490-163a-0410-bde0-09ae8108e29a
parent 0240c9c7
No related branches found
No related tags found
No related merge requests found
......@@ -987,6 +987,16 @@ class SimpleSAML_Utilities {
return $userid;
}
public static function generateRandomBytesMTrand($length) {
/* Use mt_rand to generate $length random bytes. */
$data = '';
for($i = 0; $i < $length; $i++) {
$data .= chr(mt_rand(0, 255));
}
}
/**
* This function generates a binary string containing random bytes.
......@@ -996,7 +1006,7 @@ class SimpleSAML_Utilities {
* @param $length The number of random bytes to return.
* @return A string of lenght $length with random bytes.
*/
public static function generateRandomBytes($length) {
public static function generateRandomBytes($length, $fallback = TRUE) {
static $fp = NULL;
assert('is_int($length)');
......@@ -1011,14 +1021,16 @@ class SimpleSAML_Utilities {
throw new Exception('Error reading random data.');
}
if(strlen($data) != $length) {
throw new Exception('Did not get requested number of bytes from random source.');
SimpleSAML_Logger::warning('Did not get requested number of bytes from random source. Requested (' . $length . ') got (' . strlen($data) . ')');
if ($fallback) {
$data = self::generateRandomBytesMTrand($length);
} else {
throw new Exception('Did not get requested number of bytes from random source. Requested (' . $length . ') got (' . strlen($data) . ')');
}
}
} else {
/* Use mt_rand to generate $length random bytes. */
$data = '';
for($i = 0; $i < $length; $i++) {
$data .= chr(mt_rand(0, 255));
}
$data = self::generateRandomBytesMTrand($length);
}
return $data;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment