Example #1
0
 private static function getRandomBytes($count)
 {
     $bytes = '';
     if (function_exists('openssl_random_pseudo_bytes') && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
         // OpenSSL slow on Win
         $bytes = openssl_random_pseudo_bytes($count);
     }
     if ($bytes === '' && is_readable('/dev/urandom') && ($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
         $bytes = fread($hRand, $count);
         fclose($hRand);
     }
     if (strlen($bytes) < $count) {
         $bytes = '';
         if (self::$randomState === null) {
             self::$randomState = microtime();
             if (function_exists('getmypid')) {
                 self::$randomState .= getmypid();
             }
         }
         for ($i = 0; $i < $count; $i += 16) {
             self::$randomState = md5(microtime() . self::$randomState);
             if (PHP_VERSION >= '5') {
                 $bytes .= md5(self::$randomState, true);
             } else {
                 $bytes .= pack('H*', md5(self::$randomState));
             }
         }
         $bytes = substr($bytes, 0, $count);
     }
     return $bytes;
 }