csrp icon indicating copy to clipboard operation
csrp copied to clipboard

windows init_random

Open bigo2050 opened this issue 2 years ago • 1 comments

#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 }

bigo2050 avatar Apr 28 '23 11:04 bigo2050

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.

cocagne avatar Nov 16 '23 02:11 cocagne