3 #define _WIN32_WINNT 0x0400 /* Needed for CryptoAPI on some systems */
7 static PyObject* getRandomBytes(PyObject *self, PyObject *args)
10 HCRYPTPROV hCryptProv;
11 unsigned char* bytes = NULL;
12 PyObject* returnVal = NULL;
16 if (!PyArg_ParseTuple(args, "i", &howMany))
20 if(CryptAcquireContext(
25 CRYPT_VERIFYCONTEXT) == 0)
26 return Py_BuildValue("s#", NULL, 0);
30 bytes = malloc(howMany);
38 returnVal = Py_BuildValue("s#", NULL, 0);
40 returnVal = Py_BuildValue("s#", bytes, howMany);
43 CryptReleaseContext(hCryptProv, 0);
50 /* List of functions exported by this module */
52 static struct PyMethodDef win32prng_functions[] = {
53 {"getRandomBytes", (PyCFunction)getRandomBytes, METH_VARARGS},
54 {NULL, NULL} /* Sentinel */
58 /* Initialize this module. */
60 DL_EXPORT(void) initwin32prng(void)
62 Py_InitModule("win32prng", win32prng_functions);