1 /* cryptmodule.c - by Steve Majewski
2  */
3 
4 #include "Python.h"
5 
6 #include <sys/types.h>
7 
8 /* Module crypt */
9 
10 /*[clinic input]
11 module crypt
12 [clinic start generated code]*/
13 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
14 
15 #include "clinic/_cryptmodule.c.h"
16 
17 /*[clinic input]
18 crypt.crypt
19 
20     word: str
21     salt: str
22     /
23 
24 Hash a *word* with the given *salt* and return the hashed password.
25 
26 *word* will usually be a user's password.  *salt* (either a random 2 or 16
27 character string, possibly prefixed with $digit$ to indicate the method)
28 will be used to perturb the encryption algorithm and produce distinct
29 results for a given *word*.
30 
31 [clinic start generated code]*/
32 
33 static PyObject *
crypt_crypt_impl(PyObject * module,const char * word,const char * salt)34 crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
35 /*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
36 {
37     char *crypt_result;
38 #ifdef HAVE_CRYPT_R
39     struct crypt_data data;
40     memset(&data, 0, sizeof(data));
41     crypt_result = crypt_r(word, salt, &data);
42 #else
43     crypt_result = crypt(word, salt);
44 #endif
45     if (crypt_result == NULL) {
46         return PyErr_SetFromErrno(PyExc_OSError);
47     }
48     return Py_BuildValue("s", crypt_result);
49 }
50 
51 
52 static PyMethodDef crypt_methods[] = {
53     CRYPT_CRYPT_METHODDEF
54     {NULL,              NULL}           /* sentinel */
55 };
56 
57 static PyModuleDef_Slot _crypt_slots[] = {
58     {0, NULL}
59 };
60 
61 static struct PyModuleDef cryptmodule = {
62     PyModuleDef_HEAD_INIT,
63     "_crypt",
64     NULL,
65     0,
66     crypt_methods,
67     _crypt_slots,
68     NULL,
69     NULL,
70     NULL
71 };
72 
73 PyMODINIT_FUNC
PyInit__crypt(void)74 PyInit__crypt(void)
75 {
76     return PyModuleDef_Init(&cryptmodule);
77 }
78