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 | |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 15 | |
| 16 | /*[clinic input] |
| 17 | crypt.crypt |
| 18 | |
| 19 | word: 's' |
| 20 | salt: 's' |
| 21 | / |
| 22 | |
| 23 | Hash a *word* with the given *salt* and return the hashed password. |
| 24 | |
| 25 | *word* will usually be a user's password. *salt* (either a random 2 or 16 |
| 26 | character string, possibly prefixed with $digit$ to indicate the method) |
| 27 | will be used to perturb the encryption algorithm and produce distinct |
| 28 | results for a given *word*. |
| 29 | |
| 30 | [clinic start generated code]*/ |
| 31 | |
| 32 | PyDoc_STRVAR(crypt_crypt__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 33 | "crypt($module, word, salt, /)\n" |
| 34 | "--\n" |
| 35 | "\n" |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 36 | "Hash a *word* with the given *salt* and return the hashed password.\n" |
| 37 | "\n" |
| 38 | "*word* will usually be a user\'s password. *salt* (either a random 2 or 16\n" |
| 39 | "character string, possibly prefixed with $digit$ to indicate the method)\n" |
| 40 | "will be used to perturb the encryption algorithm and produce distinct\n" |
| 41 | "results for a given *word*."); |
| 42 | |
| 43 | #define CRYPT_CRYPT_METHODDEF \ |
| 44 | {"crypt", (PyCFunction)crypt_crypt, METH_VARARGS, crypt_crypt__doc__}, |
| 45 | |
| 46 | static PyObject * |
| 47 | crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt); |
| 48 | |
| 49 | static PyObject * |
| 50 | crypt_crypt(PyModuleDef *module, PyObject *args) |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 51 | { |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 52 | PyObject *return_value = NULL; |
| 53 | const char *word; |
| 54 | const char *salt; |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 56 | if (!PyArg_ParseTuple(args, |
| 57 | "ss:crypt", |
| 58 | &word, &salt)) |
| 59 | goto exit; |
| 60 | return_value = crypt_crypt_impl(module, word, salt); |
| 61 | |
| 62 | exit: |
| 63 | return return_value; |
| 64 | } |
| 65 | |
| 66 | static PyObject * |
| 67 | crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 68 | /*[clinic end generated code: output=3eaacdf994a6ff23 input=4d93b6d0f41fbf58]*/ |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 69 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | /* On some platforms (AtheOS) crypt returns NULL for an invalid |
| 71 | salt. Return None in that case. XXX Maybe raise an exception? */ |
| 72 | return Py_BuildValue("s", crypt(word, salt)); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Fred Drake | a664dbb | 2000-02-01 20:12:39 +0000 | [diff] [blame] | 75 | |
Roger E. Masse | 56c345b | 1996-12-09 23:14:26 +0000 | [diff] [blame] | 76 | static PyMethodDef crypt_methods[] = { |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 77 | CRYPT_CRYPT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 81 | |
| 82 | static struct PyModuleDef cryptmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | PyModuleDef_HEAD_INIT, |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 84 | "_crypt", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | NULL, |
| 86 | -1, |
| 87 | crypt_methods, |
| 88 | NULL, |
| 89 | NULL, |
| 90 | NULL, |
| 91 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 94 | PyMODINIT_FUNC |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 95 | PyInit__crypt(void) |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 96 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | return PyModule_Create(&cryptmodule); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 98 | } |