Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 1 | /* cryptmodule.c - by Steve Majewski |
| 2 | */ |
| 3 | |
Roger E. Masse | 56c345b | 1996-12-09 23:14:26 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 5 | |
| 6 | #include <sys/types.h> |
| 7 | |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 8 | /* Module crypt */ |
| 9 | |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 10 | /*[clinic input] |
| 11 | module crypt |
| 12 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 13 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/ |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 14 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 15 | #include "clinic/_cryptmodule.c.h" |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 16 | |
| 17 | /*[clinic input] |
| 18 | crypt.crypt |
| 19 | |
Serhiy Storchaka | 7e810a6 | 2015-05-30 11:09:35 +0300 | [diff] [blame] | 20 | word: str |
| 21 | salt: str |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 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 | |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 33 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 34 | crypt_crypt_impl(PyObject *module, const char *word, const char *salt) |
| 35 | /*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/ |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 36 | { |
Gregory P. Smith | 387512c | 2018-12-30 15:42:32 -0800 | [diff] [blame] | 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 |
Antonio Gutierrez | 0d3fe8a | 2019-10-08 06:22:17 +0200 | [diff] [blame] | 45 | if (crypt_result == NULL) { |
| 46 | return PyErr_SetFromErrno(PyExc_OSError); |
| 47 | } |
Gregory P. Smith | 387512c | 2018-12-30 15:42:32 -0800 | [diff] [blame] | 48 | return Py_BuildValue("s", crypt_result); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Fred Drake | a664dbb | 2000-02-01 20:12:39 +0000 | [diff] [blame] | 51 | |
Roger E. Masse | 56c345b | 1996-12-09 23:14:26 +0000 | [diff] [blame] | 52 | static PyMethodDef crypt_methods[] = { |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 53 | CRYPT_CRYPT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 57 | static PyModuleDef_Slot _crypt_slots[] = { |
| 58 | {0, NULL} |
| 59 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 60 | |
| 61 | static struct PyModuleDef cryptmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | PyModuleDef_HEAD_INIT, |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 63 | "_crypt", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | NULL, |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 65 | 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | crypt_methods, |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 67 | _crypt_slots, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | NULL, |
| 69 | NULL, |
| 70 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 73 | PyMODINIT_FUNC |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 74 | PyInit__crypt(void) |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 75 | { |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 76 | return PyModuleDef_Init(&cryptmodule); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 77 | } |