From 757d95080252769cbd91ad52fed46e7ff385dcf5 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Wed, 2 Nov 2022 08:54:52 +0100 Subject: [PATCH] add new function uniqidReal() --- inc/inc.Utils.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/inc/inc.Utils.php b/inc/inc.Utils.php index b05b5f386..4affb3e31 100644 --- a/inc/inc.Utils.php +++ b/inc/inc.Utils.php @@ -695,7 +695,7 @@ function formatComment($an) { /* {{{ */ * @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( @@ -718,7 +718,7 @@ function commandExists ($command) { } return false; -} +} /* }}} */ /** * Send a file from disk to the browser @@ -851,7 +851,7 @@ function seed_pass_verify($password, $hash) { /* {{{ */ return (md5($password) == $hash) || password_verify($password, $hash); } /* }}} */ -function resolveTask($task) { +function resolveTask($task) { /* {{{ */ global $dms, $user, $settings, $logger, $fulltextservice, $notifier, $conversionmgr; if(is_object($task)) @@ -862,7 +862,7 @@ function resolveTask($task) { } } return $task; -} +} /* }}} */ /** * Return nonce for CSP @@ -880,6 +880,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 *