cpython/Modules/_cryptmodule.c

75 lines
1.5 KiB
C
Raw Normal View History

1994-05-06 11:25:39 -03:00
/* cryptmodule.c - by Steve Majewski
*/
1996-12-09 19:14:26 -04:00
#include "Python.h"
1994-05-06 11:25:39 -03:00
#include <sys/types.h>
/* Module crypt */
2014-01-14 16:00:27 -04:00
/*[clinic input]
module crypt
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
1994-05-06 11:25:39 -03:00
#include "clinic/_cryptmodule.c.h"
2014-01-14 16:00:27 -04:00
/*[clinic input]
crypt.crypt
word: str
salt: str
2014-01-14 16:00:27 -04:00
/
Hash a *word* with the given *salt* and return the hashed password.
*word* will usually be a user's password. *salt* (either a random 2 or 16
character string, possibly prefixed with $digit$ to indicate the method)
will be used to perturb the encryption algorithm and produce distinct
results for a given *word*.
[clinic start generated code]*/
static PyObject *
crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
/*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
2014-01-14 16:00:27 -04:00
{
char *crypt_result;
#ifdef HAVE_CRYPT_R
struct crypt_data data;
memset(&data, 0, sizeof(data));
crypt_result = crypt_r(word, salt, &data);
#else
crypt_result = crypt(word, salt);
#endif
if (crypt_result == NULL) {
return PyErr_SetFromErrno(PyExc_OSError);
}
return Py_BuildValue("s", crypt_result);
1994-05-06 11:25:39 -03:00
}
1996-12-09 19:14:26 -04:00
static PyMethodDef crypt_methods[] = {
2014-01-14 16:00:27 -04:00
CRYPT_CRYPT_METHODDEF
{NULL, NULL} /* sentinel */
1994-05-06 11:25:39 -03:00
};
static struct PyModuleDef cryptmodule = {
PyModuleDef_HEAD_INIT,
"_crypt",
NULL,
-1,
crypt_methods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit__crypt(void)
1994-05-06 11:25:39 -03:00
{
return PyModule_Create(&cryptmodule);
1994-05-06 11:25:39 -03:00
}