public static function initRandom() { /* Look for a system-provided source of randomness, which is usually crytographically secure. /dev/urandom is tried first because tests suggest CAPICOM is quite slow to initialize. */ if (is_readable('/dev/urandom')) { self::$randomSource = fopen('/dev/urandom', 'rb'); self::$randomFunc = 'randomFRead'; } else { if (class_exists('COM', 0)) { try { self::$randomSource = new COM('CAPICOM.Utilities.1'); // See http://msdn.microsoft.com/en-us/library/aa388182(VS.85).aspx self::$randomFunc = 'randomCOM'; } catch (Exception $e) { } } } return self::$randomFunc; }
public static function initRandom($how = NULL) { /* Look for a system-provided source of randomness, which is usually crytographically secure. /dev/urandom is tried first because tests suggest it is faster than other options. */ if ($how === NULL) { if (self::$randomFunc != self::randChoose) { return self::$randomFunc; } else { if (is_readable('/dev/urandom')) { $how = self::randDev; } else { if (function_exists('openssl_random_pseudo_bytes')) { $how = self::randOpenSSL; } else { if (function_exists('mcrypt_create_iv')) { $how = self::randMcrypt; } else { $how = self::randCAPICOM; } } } } try { self::initRandom($how); } catch (Exception $e) { self::$randomFunc = self::randPoor; } } else { $source = NULL; switch ($how) { case self::randChoose: self::$randomFunc = $how; return self::initRandom(); case self::randPoor: self::$randomFunc = $how; break; case self::randDev: $source = @fopen('/dev/urandom', 'rb'); if (!$source) { throw new UUIDException("Randomness source is not available.", 802); } break; case self::randOpenSSL: if (!function_exists('openssl_random_pseudo_bytes')) { throw new UUIDException("Randomness source is not available.", 802); } break; case self::randMcerypt: if (!function_exists('mcrypt_create_iv')) { throw new UUIDException("Randomness source is not available.", 802); } break; case self::randCAPICOM: // See http://msdn.microsoft.com/en-us/library/aa388182(VS.85).aspx if (!class_exists('COM', 0)) { throw new UUIDException("Randomness source is not available.", 802); } try { $source = new COM('CAPICOM.Utilities.1'); } catch (Exception $e) { throw new UUIDException("Randomness source is not available.", 802, $e); } break; default: throw new UUIDException("Randomness source not implemented.", 902); } self::$randomSource = $source; self::$randomFunc = $how; } return self::$randomFunc; }