diff options
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index d8cf40e..e118237 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -7882,6 +7882,42 @@ win32_urandom(PyObject *self, PyObject *args) } #endif +#ifdef __VMS +/* Use openssl random routine */ +#include <openssl/rand.h> +PyDoc_STRVAR(vms_urandom__doc__, +"urandom(n) -> str\n\n\ +Return a string of n random bytes suitable for cryptographic use."); + +static PyObject* +vms_urandom(PyObject *self, PyObject *args) +{ + int howMany; + PyObject* result; + + /* Read arguments */ + if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) + return NULL; + if (howMany < 0) + return PyErr_Format(PyExc_ValueError, + "negative argument not allowed"); + + /* Allocate bytes */ + result = PyString_FromStringAndSize(NULL, howMany); + if (result != NULL) { + /* Get random data */ + if (RAND_pseudo_bytes((unsigned char*) + PyString_AS_STRING(result), + howMany) < 0) { + Py_DECREF(result); + return PyErr_Format(PyExc_ValueError, + "RAND_pseudo_bytes"); + } + } + return result; +} +#endif + static PyMethodDef posix_methods[] = { {"access", posix_access, METH_VARARGS, posix_access__doc__}, #ifdef HAVE_TTYNAME @@ -8175,6 +8211,9 @@ static PyMethodDef posix_methods[] = { #ifdef MS_WINDOWS {"urandom", win32_urandom, METH_VARARGS, win32_urandom__doc__}, #endif + #ifdef __VMS + {"urandom", vms_urandom, METH_VARARGS, vms_urandom__doc__}, + #endif {NULL, NULL} /* Sentinel */ }; |