Generate a random string.
Although microoptimizations are generally discouraged as they impair readability this function is ripe with microoptimizations because this function has the potential of being called a huge number of times. eg. for RSA key generation.
- Parameters
-
- Returns
- string
Definition at line 54 of file Random.php.
References $_COOKIE, $_GET, $_POST, $_SERVER, $_SESSION, $GLOBALS, $i, $key, $r, $result, and phpseclib\Crypt\Base\MODE_CTR.
Referenced by phpseclib\Net\SSH1\_connect(), phpseclib\Crypt\RSA\_convertPrivateKey(), phpseclib\Crypt\RSA\_emsa_pss_encode(), phpseclib\Net\SSH2\_key_exchange(), phpseclib\Math\BigInteger\_random_number_helper(), phpseclib\Net\SSH1\_rsa_crypt(), phpseclib\Crypt\RSA\_rsaes_oaep_encrypt(), phpseclib\Crypt\RSA\_rsaes_pkcs1_v1_5_encrypt(), phpseclib\Net\SSH1\_send_binary_packet(), phpseclib\Net\SSH2\_send_binary_packet(), and phpseclib\File\X509\sign().
56 if (version_compare(PHP_VERSION,
'7.0.0',
'>=')) {
58 return \random_bytes($length);
69 if (strtoupper(substr(PHP_OS, 0, 3)) ===
'WIN') {
72 if (extension_loaded(
'mcrypt') && function_exists(
'class_alias')) {
73 return mcrypt_create_iv($length);
88 if (extension_loaded(
'openssl') && version_compare(PHP_VERSION,
'5.3.4',
'>=')) {
89 return openssl_random_pseudo_bytes($length);
93 if (extension_loaded(
'openssl')) {
94 return openssl_random_pseudo_bytes($length);
101 $fp = @
fopen(
'/dev/urandom',
'rb');
103 if ($fp !==
true && $fp !==
false) {
104 return fread($fp, $length);
111 if (extension_loaded(
'mcrypt')) {
112 return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
134 static $crypto =
false, $v;
135 if ($crypto ===
false) {
137 $old_session_id = session_id();
138 $old_use_cookies = ini_get(
'session.use_cookies');
139 $old_session_cache_limiter = session_cache_limiter();
141 if ($old_session_id !=
'') {
142 session_write_close();
146 ini_set(
'session.use_cookies', 0);
147 session_cache_limiter(
'');
150 $v = $seed =
$_SESSION[
'seed'] = pack(
'H*', sha1(
157 serialize($_OLD_SESSION)
164 session_write_close();
167 if ($old_session_id !=
'') {
168 session_id($old_session_id);
170 ini_set(
'session.use_cookies', $old_use_cookies);
171 session_cache_limiter($old_session_cache_limiter);
173 if ($_OLD_SESSION !==
false) {
175 unset($_OLD_SESSION);
189 $key = pack(
'H*', sha1($seed .
'A'));
190 $iv = pack(
'H*', sha1($seed .
'C'));
196 case class_exists(
'\phpseclib\Crypt\AES'):
197 $crypto = new
AES(
Base::MODE_CTR);
199 case class_exists(
'\phpseclib\Crypt\Twofish'):
202 case class_exists(
'\phpseclib\Crypt\Blowfish'):
205 case class_exists(
'\phpseclib\Crypt\TripleDES'):
208 case class_exists(
'\phpseclib\Crypt\DES'):
209 $crypto = new
DES(
Base::MODE_CTR);
211 case class_exists(
'\phpseclib\Crypt\RC4'):
215 user_error(__CLASS__ .
' requires at least one symmetric cipher be loaded');
219 $crypto->setKey(
$key);
221 $crypto->enableContinuousBuffer();
235 while (strlen(
$result) < $length) {
236 $i = $crypto->encrypt(microtime());
237 $r = $crypto->encrypt(
$i ^ $v);
238 $v = $crypto->encrypt(
$r ^
$i);
241 return substr(
$result, 0, $length);
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
Pure-PHP implementation of Twofish.
Pure-PHP implementation of Blowfish.
Pure-PHP implementation of Triple DES.
Pure-PHP implementation of RC4.
Base Class for all * cipher classes.
Pure-PHP implementation of DES.
Pure-PHP implementation of AES.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.