blob: da44ef34eeab780cc5a363b04456d9e90f9946b1 [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]*/
Larry Hastings581ee362014-01-28 05:00:08 -080013/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
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__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080033"crypt($module, word, salt, /)\n"
34"--\n"
35"\n"
Antoine Pitrouf5207e62014-01-14 21:00:27 +010036"Hash a *word* with the given *salt* and return the hashed password.\n"
37"\n"
38"*word* will usually be a user\'s password. *salt* (either a random 2 or 16\n"
39"character string, possibly prefixed with $digit$ to indicate the method)\n"
40"will be used to perturb the encryption algorithm and produce distinct\n"
41"results for a given *word*.");
42
43#define CRYPT_CRYPT_METHODDEF \
44 {"crypt", (PyCFunction)crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
45
46static PyObject *
47crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt);
48
49static PyObject *
50crypt_crypt(PyModuleDef *module, PyObject *args)
Guido van Rossume4c61311994-05-06 14:25:39 +000051{
Antoine Pitrouf5207e62014-01-14 21:00:27 +010052 PyObject *return_value = NULL;
53 const char *word;
54 const char *salt;
Guido van Rossume4c61311994-05-06 14:25:39 +000055
Antoine Pitrouf5207e62014-01-14 21:00:27 +010056 if (!PyArg_ParseTuple(args,
57 "ss:crypt",
58 &word, &salt))
59 goto exit;
60 return_value = crypt_crypt_impl(module, word, salt);
61
62exit:
63 return return_value;
64}
65
66static PyObject *
67crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt)
Larry Hastings2623c8c2014-02-08 22:15:29 -080068/*[clinic end generated code: output=3eaacdf994a6ff23 input=4d93b6d0f41fbf58]*/
Antoine Pitrouf5207e62014-01-14 21:00:27 +010069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 /* On some platforms (AtheOS) crypt returns NULL for an invalid
71 salt. Return None in that case. XXX Maybe raise an exception? */
72 return Py_BuildValue("s", crypt(word, salt));
Guido van Rossume4c61311994-05-06 14:25:39 +000073}
74
Fred Drakea664dbb2000-02-01 20:12:39 +000075
Roger E. Masse56c345b1996-12-09 23:14:26 +000076static PyMethodDef crypt_methods[] = {
Antoine Pitrouf5207e62014-01-14 21:00:27 +010077 CRYPT_CRYPT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 {NULL, NULL} /* sentinel */
Guido van Rossume4c61311994-05-06 14:25:39 +000079};
80
Martin v. Löwis1a214512008-06-11 05:26:20 +000081
82static struct PyModuleDef cryptmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 PyModuleDef_HEAD_INIT,
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000084 "_crypt",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 NULL,
86 -1,
87 crypt_methods,
88 NULL,
89 NULL,
90 NULL,
91 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000092};
93
Mark Hammondfe51c6d2002-08-02 02:27:13 +000094PyMODINIT_FUNC
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000095PyInit__crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 return PyModule_Create(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000098}