Let's make (git) history!
This commit is contained in:
53
random.h
Normal file
53
random.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#ifndef RANDOM_H
|
||||
#define RANDOM_H
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
#ifdef __linux__
|
||||
#include <linux/random.h>
|
||||
#endif
|
||||
#ifndef SYS_getrandom
|
||||
#include <openssl/rand.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
// dirty hacks
|
||||
#ifdef SYS_getrandom
|
||||
inline int getrandom(void *buf, size_t buflen, unsigned int flags)
|
||||
{
|
||||
return syscall(SYS_getrandom, buf, buflen, flags);
|
||||
}
|
||||
#else
|
||||
|
||||
#if __linux__
|
||||
// ancient linux systems
|
||||
#define GRND_NONBLOCK 0
|
||||
inline int getrandom(void *buf, size_t buflen, unsigned int flags)
|
||||
{
|
||||
int result = RAND_bytes(buf, buflen);
|
||||
if(result == 1)
|
||||
{
|
||||
return (int)buflen;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
#if __OpenBSD__
|
||||
inline int getrandom(void *buf, size_t buflen, unsigned int flags)
|
||||
{
|
||||
arc4random_buf(buf, buflen);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
/* TODO: if the >=C++11 prngr are good enough, use them */
|
||||
class Random
|
||||
{
|
||||
public:
|
||||
Random();
|
||||
std::string getRandomHexString(unsigned int bytes);
|
||||
};
|
||||
|
||||
#endif // RANDOM_H
|
Reference in New Issue
Block a user