Skip to content

Commit 59de049

Browse files
committed
Standard: use if available php_random_bytes() in uniqid().
1 parent bd5ff73 commit 59de049

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

ext/standard/uniqid.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#endif
3333

3434
#include "php_lcg.h"
35+
#include "php_random.h"
3536

3637
#ifdef HAVE_GETTIMEOFDAY
3738
ZEND_TLS struct timeval prev_tv = { 0, 0 };
@@ -71,7 +72,14 @@ PHP_FUNCTION(uniqid)
7172
* digits for usecs.
7273
*/
7374
if (more_entropy) {
74-
uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg() * 10);
75+
uint32_t bytes;
76+
double seed;
77+
if (php_random_bytes_silent(&bytes, sizeof(uint32_t)) == FAILURE) {
78+
seed = php_combined_lcg() * 10;
79+
} else {
80+
seed = 1.0 / ((double) UINT32_MAX / bytes);
81+
}
82+
uniqid = strpprintf(0, "%s%08x%05x%.8F", prefix, sec, usec, seed);
7583
} else {
7684
uniqid = strpprintf(0, "%s%08x%05x", prefix, sec, usec);
7785
}

0 commit comments

Comments
 (0)