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 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | #ifdef __VMS |
| 9 | #include <openssl/des.h> |
| 10 | #endif |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 11 | |
| 12 | /* Module crypt */ |
| 13 | |
| 14 | |
Peter Schneider-Kamp | a788a7f | 2000-07-10 09:57:19 +0000 | [diff] [blame] | 15 | static PyObject *crypt_crypt(PyObject *self, PyObject *args) |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 16 | { |
| 17 | char *word, *salt; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 18 | #ifndef __VMS |
Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 19 | extern char * crypt(const char *, const char *); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 20 | #endif |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 21 | |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 22 | if (!PyArg_ParseTuple(args, "ss:crypt", &word, &salt)) { |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 23 | return NULL; |
| 24 | } |
Martin v. Löwis | f90ae20 | 2002-06-11 06:22:31 +0000 | [diff] [blame] | 25 | /* On some platforms (AtheOS) crypt returns NULL for an invalid |
| 26 | salt. Return None in that case. XXX Maybe raise an exception? */ |
| 27 | return Py_BuildValue("s", crypt(word, salt)); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 28 | |
| 29 | } |
| 30 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 31 | PyDoc_STRVAR(crypt_crypt__doc__, |
| 32 | "crypt(word, salt) -> string\n\ |
Fred Drake | a664dbb | 2000-02-01 20:12:39 +0000 | [diff] [blame] | 33 | word will usually be a user's password. salt is a 2-character string\n\ |
| 34 | which will be used to select one of 4096 variations of DES. The characters\n\ |
| 35 | in salt must be either \".\", \"/\", or an alphanumeric character. Returns\n\ |
| 36 | the hashed password as a string, which will be composed of characters from\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 37 | the same alphabet as the salt."); |
Fred Drake | a664dbb | 2000-02-01 20:12:39 +0000 | [diff] [blame] | 38 | |
| 39 | |
Roger E. Masse | 56c345b | 1996-12-09 23:14:26 +0000 | [diff] [blame] | 40 | static PyMethodDef crypt_methods[] = { |
Neal Norwitz | ba3a16c | 2002-03-31 15:27:00 +0000 | [diff] [blame] | 41 | {"crypt", crypt_crypt, METH_VARARGS, crypt_crypt__doc__}, |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 42 | {NULL, NULL} /* sentinel */ |
| 43 | }; |
| 44 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 45 | |
| 46 | static struct PyModuleDef cryptmodule = { |
| 47 | PyModuleDef_HEAD_INIT, |
| 48 | "crypt", |
| 49 | NULL, |
| 50 | -1, |
| 51 | crypt_methods, |
| 52 | NULL, |
| 53 | NULL, |
| 54 | NULL, |
| 55 | NULL |
| 56 | }; |
| 57 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 58 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 59 | PyInit_crypt(void) |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 60 | { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 61 | return PyModule_Create(&cryptmodule); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 62 | } |