add new function uniqidReal()

This commit is contained in:
Uwe Steinmann 2022-11-02 08:54:52 +01:00
parent f6ec37df74
commit 68427394ae

View File

@ -683,7 +683,7 @@ function addDirSep($str) { /* {{{ */
* @param string $command The command to check
* @return bool True if the command has been found ; otherwise, false.
*/
function commandExists ($command) {
function commandExists ($command) { /* {{{ */
$whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'command -v';
$process = proc_open(
@ -706,7 +706,7 @@ function commandExists ($command) {
}
return false;
}
} /* }}} */
/**
* Send a file from disk to the browser
@ -855,6 +855,23 @@ function createNonce() { /* {{{ */
return base64_encode($bytes);
} /* }}} */
/**
* Create a real uniqid for cryptographic purposes
*
* @ return string
*/
function uniqidReal($lenght = 13) {
// uniqid gives 13 chars, but you could adjust it to your needs.
if (function_exists("random_bytes")) {
$bytes = random_bytes(ceil($lenght / 2));
} elseif (function_exists("openssl_random_pseudo_bytes")) {
$bytes = openssl_random_pseudo_bytes(ceil($lenght / 2));
} else {
throw new Exception("no cryptographically secure random function available");
}
return substr(bin2hex($bytes), 0, $lenght);
}
/**
* Compare function for sorting users by login
*