Example #1
0
 /**
  * Provides a random 32 bit number
  * if openssl is available, it is cryptographically secure. Otherwise all available entropy is gathered.
  * @return number
  */
 public static function random()
 {
     //If openssl is present, use that to generate random.
     if (function_exists("openssl_random_pseudo_bytes") && FALSE) {
         $random32bit = (int) hexdec(bin2hex(openssl_random_pseudo_bytes(64)));
     } else {
         if (self::$randomSeed === null) {
             $entropy = 1;
             if (function_exists("posix_getpid")) {
                 $entropy *= posix_getpid();
             }
             if (function_exists("memory_get_usage")) {
                 $entropy *= memory_get_usage();
             }
             list($usec, $sec) = explode(" ", microtime());
             $usec *= 1000000;
             $entropy *= $usec;
             self::$randomSeed = $entropy;
             mt_srand(self::$randomSeed);
         }
         $random32bit = mt_rand();
     }
     return $random32bit;
 }