blob: d3d9271ab5ad77985ba1ddd78e2083b28c219303 [file] [log] [blame]
Guido van Rossume4c61311994-05-06 14:25:39 +00001/* cryptmodule.c - by Steve Majewski
2 */
3
Roger E. Masse56c345b1996-12-09 23:14:26 +00004#include "Python.h"
Guido van Rossume4c61311994-05-06 14:25:39 +00005
6#include <sys/types.h>
7
Thomas Wouters0e3f5912006-08-11 14:57:12 +00008#ifdef __VMS
9#include <openssl/des.h>
10#endif
Guido van Rossume4c61311994-05-06 14:25:39 +000011
12/* Module crypt */
13
14
Peter Schneider-Kampa788a7f2000-07-10 09:57:19 +000015static PyObject *crypt_crypt(PyObject *self, PyObject *args)
Guido van Rossume4c61311994-05-06 14:25:39 +000016{
17 char *word, *salt;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000018#ifndef __VMS
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000019 extern char * crypt(const char *, const char *);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020#endif
Guido van Rossume4c61311994-05-06 14:25:39 +000021
Neal Norwitzba3a16c2002-03-31 15:27:00 +000022 if (!PyArg_ParseTuple(args, "ss:crypt", &word, &salt)) {
Guido van Rossume4c61311994-05-06 14:25:39 +000023 return NULL;
24 }
Martin v. Löwisf90ae202002-06-11 06:22:31 +000025 /* 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 Rossume4c61311994-05-06 14:25:39 +000028
29}
30
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031PyDoc_STRVAR(crypt_crypt__doc__,
32"crypt(word, salt) -> string\n\
Fred Drakea664dbb2000-02-01 20:12:39 +000033word will usually be a user's password. salt is a 2-character string\n\
34which will be used to select one of 4096 variations of DES. The characters\n\
35in salt must be either \".\", \"/\", or an alphanumeric character. Returns\n\
36the hashed password as a string, which will be composed of characters from\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000037the same alphabet as the salt.");
Fred Drakea664dbb2000-02-01 20:12:39 +000038
39
Roger E. Masse56c345b1996-12-09 23:14:26 +000040static PyMethodDef crypt_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +000041 {"crypt", crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
Guido van Rossume4c61311994-05-06 14:25:39 +000042 {NULL, NULL} /* sentinel */
43};
44
Martin v. Löwis1a214512008-06-11 05:26:20 +000045
46static 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 Hammondfe51c6d2002-08-02 02:27:13 +000058PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +000059PyInit_crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000060{
Martin v. Löwis1a214512008-06-11 05:26:20 +000061 return PyModule_Create(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000062}