ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Auth_OpenID_CryptUtil Class Reference
+ Collaboration diagram for Auth_OpenID_CryptUtil:

Static Public Member Functions

static getBytes ($num_bytes)
 Get the specified number of random bytes.
static randomString ($length, $population=null)
 Produce a string of length random bytes, chosen from chrs.

Detailed Description

Definition at line 26 of file CryptUtil.php.

Member Function Documentation

static Auth_OpenID_CryptUtil::getBytes (   $num_bytes)
static

Get the specified number of random bytes.

Attempts to use a cryptographically secure (not predictable) source of randomness if available. If there is no high-entropy randomness source available, it will fail. As a last resort, for non-critical systems, define Auth_OpenID_RAND_SOURCE as null, and the code will fall back on a pseudo-random number generator.

Parameters
int$num_bytesThe length of the return value
Returns
string $bytes random bytes

Definition at line 40 of file CryptUtil.php.

Referenced by Auth_OpenID_Signatory\createAssociation(), Auth_OpenID_MathLibrary\rand(), and randomString().

{
static $f = null;
$bytes = '';
if ($f === null) {
if (Auth_OpenID_RAND_SOURCE === null) {
$f = false;
} else {
$f = @fopen(Auth_OpenID_RAND_SOURCE, "r");
if ($f === false) {
$msg = 'Define Auth_OpenID_RAND_SOURCE as null to ' .
' continue with an insecure random number generator.';
trigger_error($msg, E_USER_ERROR);
}
}
}
if ($f === false) {
// pseudorandom used
$bytes = '';
for ($i = 0; $i < $num_bytes; $i += 4) {
$bytes .= pack('L', mt_rand());
}
$bytes = substr($bytes, 0, $num_bytes);
} else {
$bytes = fread($f, $num_bytes);
}
return $bytes;
}

+ Here is the caller graph for this function:

static Auth_OpenID_CryptUtil::randomString (   $length,
  $population = null 
)
static

Produce a string of length random bytes, chosen from chrs.

If $chrs is null, the resulting string may contain any characters.

Parameters
integer$lengthThe length of the resulting randomly-generated string
string$chrsA string of characters from which to choose to build the new string
Returns
string $result A string of randomly-chosen characters from $chrs

Definition at line 80 of file CryptUtil.php.

References $n, and getBytes().

{
if ($population === null) {
}
$popsize = strlen($population);
if ($popsize > 256) {
$msg = 'More than 256 characters supplied to ' . __FUNCTION__;
trigger_error($msg, E_USER_ERROR);
}
$duplicate = 256 % $popsize;
$str = "";
for ($i = 0; $i < $length; $i++) {
do {
} while ($n < $duplicate);
$n %= $popsize;
$str .= $population[$n];
}
return $str;
}

+ Here is the call graph for this function:


The documentation for this class was generated from the following file: