-
Notifications
You must be signed in to change notification settings - Fork 0
/
rng.cc
41 lines (36 loc) · 921 Bytes
/
rng.cc
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
#include "rng.hpp"
#include <openssl/rand.h>
#include <time.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
void dfterm::seedRNG()
{
int rng_counter = 100000;
/* Seed random number generator a bit */
while(!RAND_status() && rng_counter > 0)
{
rng_counter--;
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
RAND_screen();
DWORD t = GetTickCount();
RAND_add((void*) &t, sizeof(t), sizeof(t) / 2);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
RAND_add((void*) &tv, sizeof(tv), sizeof(tv) / 4);
#endif
}
}
void dfterm::makeRandomBytes(unsigned char* output, int output_size)
{
if (!RAND_status()) seedRNG();
if (!RAND_bytes(output, output_size))
{
seedRNG();
if (!RAND_bytes(output, output_size))
RAND_pseudo_bytes(output, output_size);
}
}