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