ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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. More...
 
static randomString ($length, $population=null)
 Produce a string of length random bytes, chosen from chrs. More...
 

Detailed Description

Definition at line 26 of file CryptUtil.php.

Member Function Documentation

◆ getBytes()

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.

41 {
42 static $f = null;
43 $bytes = '';
44 if ($f === null) {
45 if (Auth_OpenID_RAND_SOURCE === null) {
46 $f = false;
47 } else {
48 $f = @fopen(Auth_OpenID_RAND_SOURCE, "r");
49 if ($f === false) {
50 $msg = 'Define Auth_OpenID_RAND_SOURCE as null to ' .
51 ' continue with an insecure random number generator.';
52 trigger_error($msg, E_USER_ERROR);
53 }
54 }
55 }
56 if ($f === false) {
57 // pseudorandom used
58 $bytes = '';
59 for ($i = 0; $i < $num_bytes; $i += 4) {
60 $bytes .= pack('L', mt_rand());
61 }
62 $bytes = substr($bytes, 0, $num_bytes);
63 } else {
64 $bytes = fread($f, $num_bytes);
65 }
66 return $bytes;
67 }

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

+ Here is the caller graph for this function:

◆ randomString()

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.

81 {
82 if ($population === null) {
83 return Auth_OpenID_CryptUtil::getBytes($length);
84 }
85
86 $popsize = strlen($population);
87
88 if ($popsize > 256) {
89 $msg = 'More than 256 characters supplied to ' . __FUNCTION__;
90 trigger_error($msg, E_USER_ERROR);
91 }
92
93 $duplicate = 256 % $popsize;
94
95 $str = "";
96 for ($i = 0; $i < $length; $i++) {
97 do {
99 } while ($n < $duplicate);
100
101 $n %= $popsize;
102 $str .= $population[$n];
103 }
104
105 return $str;
106 }
$n
Definition: RandomTest.php:80
static getBytes($num_bytes)
Get the specified number of random bytes.
Definition: CryptUtil.php:40

References $n, and getBytes().

+ Here is the call graph for this function:

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