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.
55 {
56 if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
57 try {
58 return \random_bytes($length);
59 } catch (\Throwable $e) {
60
61
62
63
64
65
66 }
67 }
68
69 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
70
71
72 if (extension_loaded('mcrypt') && function_exists('class_alias')) {
73 return mcrypt_create_iv($length);
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88 if (extension_loaded('openssl') && version_compare(PHP_VERSION, '5.3.4', '>=')) {
89 return openssl_random_pseudo_bytes($length);
90 }
91 } else {
92
93 if (extension_loaded('openssl')) {
94 return openssl_random_pseudo_bytes($length);
95 }
96
97 static $fp = true;
98 if ($fp === true) {
99
100
101 $fp = @fopen('/dev/urandom', 'rb');
102 }
103 if ($fp !== true && $fp !== false) {
104 return fread($fp, $length);
105 }
106
107
108
109
110
111 if (extension_loaded('mcrypt')) {
112 return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 static $crypto = false, $v;
135 if ($crypto === false) {
136
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();
143 }
144
145 session_id(1);
146 ini_set('session.use_cookies', 0);
147 session_cache_limiter('');
148 session_start();
149
150 $v = $seed =
$_SESSION[
'seed'] = pack(
'H*', sha1(
157 serialize($_OLD_SESSION)
158 ));
161 }
163
164 session_write_close();
165
166
167 if ($old_session_id != '') {
168 session_id($old_session_id);
169 session_start();
170 ini_set('session.use_cookies', $old_use_cookies);
171 session_cache_limiter($old_session_cache_limiter);
172 } else {
173 if ($_OLD_SESSION !== false) {
175 unset($_OLD_SESSION);
176 } else {
178 }
179 }
180
181
182
183
184
185
186
187
188
189 $key = pack(
'H*', sha1($seed .
'A'));
190 $iv = pack('H*', sha1($seed . 'C'));
191
192
193
194
195 switch (true) {
196 case class_exists('\phpseclib\Crypt\AES'):
197 $crypto = new
AES(
Base::MODE_CTR);
198 break;
199 case class_exists('\phpseclib\Crypt\Twofish'):
201 break;
202 case class_exists('\phpseclib\Crypt\Blowfish'):
204 break;
205 case class_exists('\phpseclib\Crypt\TripleDES'):
207 break;
208 case class_exists('\phpseclib\Crypt\DES'):
209 $crypto = new
DES(
Base::MODE_CTR);
210 break;
211 case class_exists('\phpseclib\Crypt\RC4'):
213 break;
214 default:
215 user_error(__CLASS__ . ' requires at least one symmetric cipher be loaded');
216 return false;
217 }
218
219 $crypto->setKey(
$key);
220 $crypto->setIV($iv);
221 $crypto->enableContinuousBuffer();
222 }
223
224
225
226
227
228
229
230
231
232
233
235 while (strlen(
$result) < $length) {
236 $i = $crypto->encrypt(microtime());
237 $r = $crypto->encrypt(
$i ^ $v);
238 $v = $crypto->encrypt(
$r ^
$i);
240 }
241 return substr(
$result, 0, $length);
242 }
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
Pure-PHP implementation of AES.
Base Class for all \phpseclib\Crypt* cipher classes.
Pure-PHP implementation of Blowfish.
Pure-PHP implementation of DES.
Pure-PHP implementation of RC4.
Pure-PHP implementation of Triple DES.
Pure-PHP implementation of Twofish.
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']