blob: af502cf5ecea0a67624c936a94496e025fa97744 [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
Guido van Rossume4c61311994-05-06 14:25:39 +00008/* Module crypt */
9
Antoine Pitrouf5207e62014-01-14 21:00:27 +010010/*[clinic input]
11module crypt
12[clinic start generated code]*/
13/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
Guido van Rossume4c61311994-05-06 14:25:39 +000014
Antoine Pitrouf5207e62014-01-14 21:00:27 +010015
16/*[clinic input]
17crypt.crypt
18
19 word: 's'
20 salt: 's'
21 /
22
23Hash 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
26character string, possibly prefixed with $digit$ to indicate the method)
27will be used to perturb the encryption algorithm and produce distinct
28results for a given *word*.
29
30[clinic start generated code]*/
31
32PyDoc_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
44static PyObject *
45crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt);
46
47static PyObject *
48crypt_crypt(PyModuleDef *module, PyObject *args)
Guido van Rossume4c61311994-05-06 14:25:39 +000049{
Antoine Pitrouf5207e62014-01-14 21:00:27 +010050 PyObject *return_value = NULL;
51 const char *word;
52 const char *salt;
Guido van Rossume4c61311994-05-06 14:25:39 +000053
Antoine Pitrouf5207e62014-01-14 21:00:27 +010054 if (!PyArg_ParseTuple(args,
55 "ss:crypt",
56 &word, &salt))
57 goto exit;
58 return_value = crypt_crypt_impl(module, word, salt);
59
60exit:
61 return return_value;
62}
63
64static PyObject *
65crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt)
66/*[clinic end generated code: checksum=a137540bf6862f9935fc112b8bb1d62d6dd1ad02]*/
67{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 /* 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 Rossume4c61311994-05-06 14:25:39 +000071}
72
Fred Drakea664dbb2000-02-01 20:12:39 +000073
Roger E. Masse56c345b1996-12-09 23:14:26 +000074static PyMethodDef crypt_methods[] = {
Antoine Pitrouf5207e62014-01-14 21:00:27 +010075 CRYPT_CRYPT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 {NULL, NULL} /* sentinel */
Guido van Rossume4c61311994-05-06 14:25:39 +000077};
78
Martin v. Löwis1a214512008-06-11 05:26:20 +000079
80static struct PyModuleDef cryptmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 PyModuleDef_HEAD_INIT,
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000082 "_crypt",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 NULL,
84 -1,
85 crypt_methods,
86 NULL,
87 NULL,
88 NULL,
89 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000090};
91
Mark Hammondfe51c6d2002-08-02 02:27:13 +000092PyMODINIT_FUNC
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000093PyInit__crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return PyModule_Create(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000096}