1994-05-06 16:25:39 +02:00
|
|
|
/* cryptmodule.c - by Steve Majewski
|
|
|
|
*/
|
|
|
|
|
1996-12-10 00:14:26 +01:00
|
|
|
#include "Python.h"
|
1994-05-06 16:25:39 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2021-07-28 00:58:12 +02:00
|
|
|
#ifdef HAVE_CRYPT_H
|
|
|
|
#include <crypt.h>
|
|
|
|
#endif
|
1994-05-06 16:25:39 +02:00
|
|
|
|
|
|
|
/* Module crypt */
|
|
|
|
|
2014-01-14 21:00:27 +01:00
|
|
|
/*[clinic input]
|
|
|
|
module crypt
|
|
|
|
[clinic start generated code]*/
|
2014-01-28 14:00:08 +01:00
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
|
1994-05-06 16:25:39 +02:00
|
|
|
|
2015-04-03 22:53:51 +02:00
|
|
|
#include "clinic/_cryptmodule.c.h"
|
2014-01-14 21:00:27 +01:00
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
crypt.crypt
|
|
|
|
|
2015-05-30 10:09:35 +02:00
|
|
|
word: str
|
|
|
|
salt: str
|
2014-01-14 21:00:27 +01: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 *
|
2016-07-07 16:35:15 +02:00
|
|
|
crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
|
|
|
|
/*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
|
2014-01-14 21:00:27 +01:00
|
|
|
{
|
2018-12-31 00:42:32 +01: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
|
2019-10-08 06:22:17 +02:00
|
|
|
if (crypt_result == NULL) {
|
|
|
|
return PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
}
|
2018-12-31 00:42:32 +01:00
|
|
|
return Py_BuildValue("s", crypt_result);
|
1994-05-06 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2000-02-01 21:12:39 +01:00
|
|
|
|
1996-12-10 00:14:26 +01:00
|
|
|
static PyMethodDef crypt_methods[] = {
|
2014-01-14 21:00:27 +01:00
|
|
|
CRYPT_CRYPT_METHODDEF
|
2010-05-09 17:52:27 +02:00
|
|
|
{NULL, NULL} /* sentinel */
|
1994-05-06 16:25:39 +02:00
|
|
|
};
|
|
|
|
|
2020-02-17 10:11:34 +01:00
|
|
|
static PyModuleDef_Slot _crypt_slots[] = {
|
2023-05-05 23:11:27 +02:00
|
|
|
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
2020-02-17 10:11:34 +01:00
|
|
|
{0, NULL}
|
|
|
|
};
|
2008-06-11 07:26:20 +02:00
|
|
|
|
|
|
|
static struct PyModuleDef cryptmodule = {
|
2010-05-09 17:52:27 +02:00
|
|
|
PyModuleDef_HEAD_INIT,
|
2011-02-22 11:55:44 +01:00
|
|
|
"_crypt",
|
2010-05-09 17:52:27 +02:00
|
|
|
NULL,
|
2020-02-17 10:11:34 +01:00
|
|
|
0,
|
2010-05-09 17:52:27 +02:00
|
|
|
crypt_methods,
|
2020-02-17 10:11:34 +01:00
|
|
|
_crypt_slots,
|
2010-05-09 17:52:27 +02:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2008-06-11 07:26:20 +02:00
|
|
|
};
|
|
|
|
|
2002-08-02 04:27:13 +02:00
|
|
|
PyMODINIT_FUNC
|
2011-02-22 11:55:44 +01:00
|
|
|
PyInit__crypt(void)
|
1994-05-06 16:25:39 +02:00
|
|
|
{
|
2020-02-17 10:11:34 +01:00
|
|
|
return PyModuleDef_Init(&cryptmodule);
|
1994-05-06 16:25:39 +02:00
|
|
|
}
|