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     return Py_BuildValue("s", crypt_result);
46 }
47 
48 
49 static PyMethodDef crypt_methods[] = {
50     CRYPT_CRYPT_METHODDEF
51     {NULL,              NULL}           /* sentinel */
52 };
53 
54 
55 static struct PyModuleDef cryptmodule = {
56     PyModuleDef_HEAD_INIT,
57     "_crypt",
58     NULL,
59     -1,
60     crypt_methods,
61     NULL,
62     NULL,
63     NULL,
64     NULL
65 };
66 
67 PyMODINIT_FUNC
PyInit__crypt(void)68 PyInit__crypt(void)
69 {
70     return PyModule_Create(&cryptmodule);
71 }
72