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