mirror of
https://github.com/friendica/friendica
synced 2024-12-27 08:25:13 +01:00
Add odd number of character support to random_string()
This commit is contained in:
parent
d38c040d50
commit
d5c38b9fc9
1 changed files with 4 additions and 11 deletions
|
@ -41,26 +41,19 @@ function replace_macros($s, $r) {
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PHP < 7 polyfill
|
|
||||||
if (!is_callable('intdiv')) {
|
|
||||||
function intdiv($a, $b) {
|
|
||||||
return ($a - $a % $b) / $b;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Generates a pseudo-random string of hexadecimal characters
|
* @brief Generates a pseudo-random string of hexadecimal characters
|
||||||
*
|
*
|
||||||
* Only supports pair numbers of output characters.
|
|
||||||
*
|
|
||||||
* @param int $size
|
* @param int $size
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function random_string($size = 64)
|
function random_string($size = 64)
|
||||||
{
|
{
|
||||||
$bytes = random_bytes(intdiv((int) $size, 2));
|
$byte_size = ceil($size / 2);
|
||||||
|
|
||||||
$return = bin2hex($bytes);
|
$bytes = random_bytes($byte_size);
|
||||||
|
|
||||||
|
$return = substr(bin2hex($bytes), 0, $size);
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue