62 if (function_exists(
'openssl_random_pseudo_bytes') && substr(PHP_OS, 0, 3) !==
'WIN') {
63 $SSLstr = openssl_random_pseudo_bytes($length, $strong);
74 if (function_exists(
'mcrypt_create_iv') && substr(PHP_OS, 0, 3) !==
'WIN') {
75 $str = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
90 $msec_per_round = 400;
94 $handle = @fopen(
'/dev/urandom',
'rb');
95 if ($handle && function_exists(
'stream_set_read_buffer')) {
96 @stream_set_read_buffer($handle, 0);
100 $bytes = ($total > $hash_len) ? $hash_len : $total;
104 $entropy = rand() . uniqid(mt_rand(),
true) . $SSLstr;
105 $entropy .= implode(
'', @fstat(@fopen(__FILE__,
'r')));
106 $entropy .= memory_get_usage() . getmypid();
107 $entropy .= serialize($_ENV) . serialize($_SERVER);
108 if (function_exists(
'posix_times')) {
109 $entropy .= serialize(posix_times());
111 if (function_exists(
'zend_thread_id')) {
112 $entropy .= zend_thread_id();
116 $entropy .= @fread($handle, $bytes);
119 for ($i = 0; $i < 3; $i++) {
120 $c1 = microtime(
true);
121 $var = sha1(mt_rand());
122 for ($j = 0; $j < 50; $j++) {
125 $c2 = microtime(
true);
126 $entropy .= $c1 . $c2;
131 $rounds = (int) ($msec_per_round * 50 / (
int) (($c2 - $c1) * 1000000));
135 $iter = $bytes * (int) (ceil(8 / $bits_per_round));
137 for ($i = 0; $i < $iter; $i++) {
139 $var = sha1(mt_rand());
140 for ($j = 0; $j < $rounds; $j++) {
144 $entropy .= $c1 . $c2;
149 $str .= sha1($entropy,
true);
151 }
while ($length >
strlen($str));
157 return substr($str, 0, $length);
197 throw new \InvalidArgumentException(
'Length should be >= 1');
201 $numBytes = ceil($length * 0.75);
203 $string = substr(rtrim(base64_encode($bytes),
'='), 0, $length);
206 return strtr(
$string,
'+/',
'-_');
209 if ($chars == self::CHARS_HEX) {
212 return substr(bin2hex($bytes), 0, $length);
215 $listLen =
strlen($chars);
218 return str_repeat($chars, $length);
224 for ($i = 0; $i < $length; $i++) {
225 $pos = ($pos + ord($bytes[$i])) % $listLen;
246 $len1 = $this->
strlen($str1);
247 $len2 = $this->
strlen($str2);
248 if ($len1 !== $len2) {
253 for ($i = 0; $i < $len1; $i++) {
254 $status |= (ord($str1[$i]) ^ ord($str2[$i]));
257 return $status === 0;
278 protected function strlen($binary_string) {
279 if (function_exists(
'mb_strlen')) {
280 return mb_strlen($binary_string,
'8bit');
282 return strlen($binary_string);
areEqual($str1, $str2)
Are two strings equal (compared in constant time)?
const CHARS_PASSWORD
Character set for temp passwords (no risk of embedded profanity/glyphs that look similar) ...
getRandomBytes($length)
Generate a string of highly randomized bytes (over the full 8-bit range).
Component for creating HMAC tokens.
const CHARS_HEX
Character set for hexadecimal.
getRandomString($length, $chars=null)
Generate a random string of specified length.
strlen($binary_string)
Count the number of bytes in a string.
getHmac($data, $algo= 'sha256', $key= '')
Get an HMAC token builder/validator object.