csrp
csrp copied to clipboard
windows init_random
#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 }
Not sure how I missed this issue. I'm not sure I understand the problem and/or solution this issue is intended to convey. Could you turn it into a pull request instead? Admittedly I don't do much Windows development so it's likely that your approach is better. I just can't make it out from the issue text.