-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrandom.cpp
54 lines (45 loc) · 929 Bytes
/
random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
class RandomGen {
private:
int Fd_;
public:
RandomGen();
int getRandBytes(char *bytes);
int getRandBytes(short *bytes);
int getRandBytes(int *bytes);
int getRandBytes(uint8_t *bytes, int len);
~RandomGen();
};
RandomGen::RandomGen()
{
Fd_ = open("/dev/urandom", O_RDONLY);
if (Fd_ < 0) {
return;
}
}
RandomGen::~RandomGen()
{
if (Fd_ > 0) {
close(Fd_);
}
}
int RandomGen::getRandBytes(char *bytes)
{
return read(Fd_, bytes, sizeof(char));
}
int RandomGen::getRandBytes(short *bytes)
{
return read(Fd_, bytes, sizeof(short));
}
int RandomGen::getRandBytes(int *bytes)
{
return read(Fd_, bytes, sizeof(int));
}
int RandomGen::getRandBytes(uint8_t *bytes, int len)
{
return read(Fd_, bytes, len);
}