core: add random number generator
This commit is contained in:
35
core/sys/darwin/random.c
Normal file
35
core/sys/darwin/random.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
uint64_t z__b_platform_random_seed()
|
||||
{
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
uint64_t v = 0;
|
||||
if (read(fd, &v, sizeof v) != sizeof v) {
|
||||
close(fd);
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
uint64_t z__b_platform_random_seed_secure()
|
||||
{
|
||||
int fd = open("/dev/random", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
uint64_t v = 0;
|
||||
if (read(fd, &v, sizeof v) != sizeof v) {
|
||||
close(fd);
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
35
core/sys/linux/random.c
Normal file
35
core/sys/linux/random.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
uint64_t z__c_platform_random_seed()
|
||||
{
|
||||
int fd = open("/dev/urandom", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
uint64_t v = 0;
|
||||
if (read(fd, &v, sizeof v) != sizeof v) {
|
||||
close(fd);
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
uint64_t z__c_platform_random_seed_secure()
|
||||
{
|
||||
int fd = open("/dev/random", O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
uint64_t v = 0;
|
||||
if (read(fd, &v, sizeof v) != sizeof v) {
|
||||
close(fd);
|
||||
return (uint64_t)-1;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
Reference in New Issue
Block a user