Skip to content

Use getentropy in splitmix_init on unix-like platforms #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 8 additions & 34 deletions cbits-unix/init.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
#include <stdint.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

uint64_t splitmix_init() {

/* if there is /dev/urandom, read from it */
FILE *urandom = fopen("/dev/urandom", "r");
if (urandom) {
uint64_t result = 0;
size_t r = fread(&result, sizeof(uint64_t), 1, urandom);
fclose(urandom);

if (r == 1) {
return result;
} else {
return 0xfeed1000;
}

} else {
/* time of day */
struct timeval tp = {0, 0};
gettimeofday(&tp, NULL);
/* for macos */
#ifdef __APPLE__
#include <sys/random.h>
#endif

/* cputime */
clock_t c = clock();

/* process id */
pid_t p = getpid();

return ((uint64_t) tp.tv_sec)
^ ((uint64_t) tp.tv_usec)
^ ((uint64_t) c << 16)
^ ((uint64_t) p << 32);
}
uint64_t splitmix_init() {
uint64_t result;
int r = getentropy(&result, sizeof(uint64_t));
return r == 0 ? result : 0xfeed1000;
}
Loading