3 use \Elgg\Database\SiteSecret;
36 $this->site_secret = $site_secret;
73 if (is_callable(
'random_bytes')) {
85 if (function_exists(
'openssl_random_pseudo_bytes') && substr(PHP_OS, 0, 3) !==
'WIN') {
86 $SSLstr = openssl_random_pseudo_bytes(
$length, $strong);
97 if (function_exists(
'mcrypt_create_iv') && substr(PHP_OS, 0, 3) !==
'WIN') {
98 $str = mcrypt_create_iv(
$length, MCRYPT_DEV_URANDOM);
113 $msec_per_round = 400;
117 $handle = @fopen(
'/dev/urandom',
'rb');
118 if ($handle && function_exists(
'stream_set_read_buffer')) {
119 @stream_set_read_buffer($handle, 0);
123 $bytes = ($total > $hash_len) ? $hash_len : $total;
127 $entropy = rand() . uniqid(mt_rand(),
true) . $SSLstr;
128 $entropy .= implode(
'', @fstat(@fopen(__FILE__,
'r')));
129 $entropy .= memory_get_usage() . getmypid();
130 $entropy .= serialize($_ENV) . serialize($_SERVER);
131 if (function_exists(
'posix_times')) {
132 $entropy .= serialize(posix_times());
134 if (function_exists(
'zend_thread_id')) {
135 $entropy .= zend_thread_id();
139 $entropy .= @fread($handle, $bytes);
145 for ($i = 0; $i < 3; $i++) {
146 $c1 = microtime(
true);
147 $var = sha1(mt_rand());
148 for ($j = 0; $j < 50; $j++) {
151 $c2 = microtime(
true);
152 $entropy .= $c1 . $c2;
157 if ($c2 - $c1 == 0) {
163 $rounds = (int) ($msec_per_round * 50 / (
int) (($c2 - $c1) * 1000000));
168 $iter = $bytes * (int) (ceil(8 / $bits_per_round));
170 for ($i = 0; $i < $iter; $i++) {
172 $var = sha1(mt_rand());
173 for ($j = 0; $j < $rounds; $j++) {
177 $entropy .= $c1 . $c2;
182 $str .= sha1($entropy,
true);
190 return substr($str, 0,
$length);
204 $key = $this->site_secret->get(
true);
230 throw new \InvalidArgumentException(
'Length should be >= 1');
234 $numBytes = ceil(
$length * 0.75);
239 return strtr(
$string,
'+/',
'-_');
242 if ($chars == self::CHARS_HEX) {
245 return substr(bin2hex($bytes), 0,
$length);
248 $listLen =
strlen($chars);
251 return str_repeat($chars,
$length);
257 for ($i = 0; $i <
$length; $i++) {
258 $pos = ($pos + ord($bytes[$i])) % $listLen;
279 $len1 = $this->
strlen($str1);
280 $len2 = $this->
strlen($str2);
281 if ($len1 !== $len2) {
286 for ($i = 0; $i < $len1; $i++) {
287 $status |= (ord($str1[$i]) ^ ord($str2[$i]));
290 return $status === 0;
311 protected function strlen($binary_string) {
312 if (function_exists(
'mb_strlen')) {
313 return mb_strlen($binary_string,
'8bit');
315 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.
__construct(SiteSecret $site_secret=null)
Constructor.
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.