mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 20:00:58 +01:00
fixed linux portability, use libsodium random*() stuff
This commit is contained in:
@@ -57,39 +57,16 @@
|
|||||||
|
|
||||||
|
|
||||||
#ifndef HAVE_ARC4RANDOM_BUF
|
#ifndef HAVE_ARC4RANDOM_BUF
|
||||||
// shitty OS. we've got to use other stuff
|
// shitty OS. we're using libsodium's implementation
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <sodium.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
static inline FILE *__getranddev() {
|
|
||||||
FILE *R;
|
|
||||||
if((R = fopen("/dev/urandom", "rb")) == NULL) {
|
|
||||||
// not even this is here! what a shame
|
|
||||||
if((R = fopen("/dev/random", "rb")) == NULL) {
|
|
||||||
// not available or depleted. that's too bad
|
|
||||||
fprintf(stderr, "ERROR: /dev/urandom not available, /dev/random is depleted.\n");
|
|
||||||
fprintf(stderr, "That's horrible for you but a nightmare for me. I die. Bye.\n");
|
|
||||||
exit(2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return R;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline u_int32_t arc4random() {
|
static inline u_int32_t arc4random() {
|
||||||
uint32_t x;
|
return randombytes_random();
|
||||||
FILE *R = __getranddev();
|
|
||||||
fread(&x, sizeof(uint32_t), 1, R);
|
|
||||||
fclose(R);
|
|
||||||
return x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void arc4random_buf(void *buf, size_t nbytes) {
|
static inline void arc4random_buf(void *buf, size_t nbytes) {
|
||||||
FILE *R = __getranddev();
|
randombytes(buf, nbytes);
|
||||||
fread(buf, nbytes, 1, R);
|
|
||||||
fclose(R);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user