mirror of
https://github.com/python/cpython.git
synced 2025-08-01 23:53:15 +00:00
win32_urandom(): Rewrite to Python C standards (hard tabs, function name
in first column, no parens around return value).
This commit is contained in:
parent
38330fe5ef
commit
4ad8217ae9
1 changed files with 55 additions and 48 deletions
|
@ -7236,15 +7236,16 @@ typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV hProv, DWORD dwLen,\
|
|||
static CRYPTGENRANDOM pCryptGenRandom = NULL;
|
||||
static HCRYPTPROV hCryptProv = 0;
|
||||
|
||||
static PyObject* win32_urandom(PyObject *self, PyObject *args)
|
||||
static PyObject*
|
||||
win32_urandom(PyObject *self, PyObject *args)
|
||||
{
|
||||
int howMany = 0;
|
||||
unsigned char* bytes = NULL;
|
||||
PyObject* returnVal = NULL;
|
||||
|
||||
/* Read arguments */
|
||||
if (!PyArg_ParseTuple(args, "i", &howMany))
|
||||
return(NULL);
|
||||
if (! PyArg_ParseTuple(args, "i", &howMany))
|
||||
return NULL;
|
||||
|
||||
if (hCryptProv == 0) {
|
||||
HINSTANCE hAdvAPI32 = NULL;
|
||||
|
@ -7252,32 +7253,38 @@ static PyObject* win32_urandom(PyObject *self, PyObject *args)
|
|||
|
||||
/* Obtain handle to the DLL containing CryptoAPI
|
||||
This should not fail */
|
||||
if( (hAdvAPI32 = GetModuleHandle("advapi32.dll")) == NULL)
|
||||
hAdvAPI32 = GetModuleHandle("advapi32.dll");
|
||||
if(hAdvAPI32 == NULL)
|
||||
return win32_error("GetModuleHandle", NULL);
|
||||
|
||||
/* Obtain pointers to the CryptoAPI functions
|
||||
This will fail on some early versions of Win95 */
|
||||
pCryptAcquireContext=(CRYPTACQUIRECONTEXTA)GetProcAddress(hAdvAPI32,\
|
||||
pCryptAcquireContext = (CRYPTACQUIRECONTEXTA)GetProcAddress(
|
||||
hAdvAPI32,
|
||||
"CryptAcquireContextA");
|
||||
pCryptGenRandom=(CRYPTGENRANDOM)GetProcAddress(hAdvAPI32,\
|
||||
"CryptGenRandom");
|
||||
if (pCryptAcquireContext == NULL)
|
||||
return PyErr_Format(PyExc_NotImplementedError,
|
||||
"CryptAcquireContextA not found");
|
||||
|
||||
if (pCryptAcquireContext == NULL || pCryptGenRandom == NULL)
|
||||
return PyErr_Format(PyExc_NotImplementedError,\
|
||||
pCryptGenRandom = (CRYPTGENRANDOM)GetProcAddress(
|
||||
hAdvAPI32, "CryptGenRandom");
|
||||
if (pCryptAcquireContext == NULL)
|
||||
return PyErr_Format(PyExc_NotImplementedError,
|
||||
"CryptGenRandom not found");
|
||||
|
||||
/* Acquire context */
|
||||
if(!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT))
|
||||
if (! pCryptAcquireContext(&hCryptProv, NULL, NULL,
|
||||
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
|
||||
return win32_error("CryptAcquireContext", NULL);
|
||||
}
|
||||
|
||||
/* Allocate bytes */
|
||||
if ((bytes = (unsigned char*)PyMem_Malloc(howMany)) == NULL)
|
||||
bytes = (unsigned char*)PyMem_Malloc(howMany);
|
||||
if (bytes == NULL)
|
||||
return PyErr_NoMemory();
|
||||
|
||||
/* Get random data */
|
||||
if (!pCryptGenRandom(hCryptProv, howMany, bytes)) {
|
||||
if (! pCryptGenRandom(hCryptProv, howMany, bytes)) {
|
||||
PyMem_Free(bytes);
|
||||
return win32_error("CryptGenRandom", NULL);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue