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 * |
| 34 | crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt) |
Serhiy Storchaka | 7e810a6 | 2015-05-30 11:09:35 +0300 | [diff] [blame] | 35 | /*[clinic end generated code: output=995ad1e854d83069 input=0e8edec9c364352b]*/ |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 36 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | /* On some platforms (AtheOS) crypt returns NULL for an invalid |
| 38 | salt. Return None in that case. XXX Maybe raise an exception? */ |
| 39 | return Py_BuildValue("s", crypt(word, salt)); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Fred Drake | a664dbb | 2000-02-01 20:12:39 +0000 | [diff] [blame] | 42 | |
Roger E. Masse | 56c345b | 1996-12-09 23:14:26 +0000 | [diff] [blame] | 43 | static PyMethodDef crypt_methods[] = { |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 44 | CRYPT_CRYPT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 48 | |
| 49 | static struct PyModuleDef cryptmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | PyModuleDef_HEAD_INIT, |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 51 | "_crypt", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | NULL, |
| 53 | -1, |
| 54 | crypt_methods, |
| 55 | NULL, |
| 56 | NULL, |
| 57 | NULL, |
| 58 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 61 | PyMODINIT_FUNC |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 62 | PyInit__crypt(void) |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 63 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | return PyModule_Create(&cryptmodule); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 65 | } |