blob: 51007889bf820c07321942133fdde8adb31c535e [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 char *word, *salt;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000018#ifndef __VMS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 if (!PyArg_ParseTuple(args, "ss:crypt", &word, &salt)) {
23 return NULL;
24 }
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 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[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 {"crypt", crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
42 {NULL, NULL} /* sentinel */
Guido van Rossume4c61311994-05-06 14:25:39 +000043};
44
Martin v. Löwis1a214512008-06-11 05:26:20 +000045
46static struct PyModuleDef cryptmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 PyModuleDef_HEAD_INIT,
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000048 "_crypt",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 NULL,
50 -1,
51 crypt_methods,
52 NULL,
53 NULL,
54 NULL,
55 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000056};
57
Mark Hammondfe51c6d2002-08-02 02:27:13 +000058PyMODINIT_FUNC
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000059PyInit__crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 return PyModule_Create(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000062}