-
Notifications
You must be signed in to change notification settings - Fork 36
Open
Description
#include <windows.h>
#include <bcrypt.h>
#include <ntstatus.h>
#include <wincred.h>
static void init_random()
{
unsigned char buff[64];
if (g_initialized)
return;
#ifdef WIN32
{
BCRYPT_ALG_HANDLE hAlg = NULL;
void* hRng = NULL;
NTSTATUS status = 0;
status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, NULL, 0);
if (status != STATUS_SUCCESS) {
goto cleanup;
}
status = BCryptGenRandom(hAlg, buff, sizeof(buff), 0);
if (status != STATUS_SUCCESS) {
goto cleanup;
}
g_initialized = 1;
RAND_seed(buff, sizeof(buff));
cleanup:
if (hRng) {
BCryptCloseAlgorithmProvider((BCRYPT_ALG_HANDLE)hRng, 0);
}
if (hAlg) {
BCryptCloseAlgorithmProvider(hAlg, 0);
}
}
#else
{
FILE* fp = fopen("/dev/urandom", "rb");
if (fp) {
size_t read = fread(buff, sizeof(buff), 1, fp);
g_initialized = read == 1;
fclose(fp);
}
}
if (g_initialized)
RAND_seed(buff, sizeof(buff));
#endif
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels