blob: 6377f8430beadc58972ddffea92b0829b05ce394 [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
Mark Hammondfe51c6d2002-08-02 02:27:13 +000045PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000046initcrypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000047{
Roger E. Masse56c345b1996-12-09 23:14:26 +000048 Py_InitModule("crypt", crypt_methods);
Guido van Rossume4c61311994-05-06 14:25:39 +000049}