blob: 5d03f45f643615315ddaf20758c0dcd659e6a6db [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
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030015#include "clinic/_cryptmodule.c.h"
Antoine Pitrouf5207e62014-01-14 21:00:27 +010016
17/*[clinic input]
18crypt.crypt
19
Serhiy Storchaka7e810a62015-05-30 11:09:35 +030020 word: str
21 salt: str
Antoine Pitrouf5207e62014-01-14 21:00:27 +010022 /
23
24Hash a *word* with the given *salt* and return the hashed password.
25
26*word* will usually be a user's password. *salt* (either a random 2 or 16
27character string, possibly prefixed with $digit$ to indicate the method)
28will be used to perturb the encryption algorithm and produce distinct
29results for a given *word*.
30
31[clinic start generated code]*/
32
Antoine Pitrouf5207e62014-01-14 21:00:27 +010033static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030034crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
35/*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
Antoine Pitrouf5207e62014-01-14 21:00:27 +010036{
Gregory P. Smith387512c2018-12-30 15:42:32 -080037 char *crypt_result;
38#ifdef HAVE_CRYPT_R
39 struct crypt_data data;
40 memset(&data, 0, sizeof(data));
41 crypt_result = crypt_r(word, salt, &data);
42#else
43 crypt_result = crypt(word, salt);
44#endif
45 return Py_BuildValue("s", crypt_result);
Guido van Rossume4c61311994-05-06 14:25:39 +000046}
47
Fred Drakea664dbb2000-02-01 20:12:39 +000048
Roger E. Masse56c345b1996-12-09 23:14:26 +000049static PyMethodDef crypt_methods[] = {
Antoine Pitrouf5207e62014-01-14 21:00:27 +010050 CRYPT_CRYPT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 {NULL, NULL} /* sentinel */
Guido van Rossume4c61311994-05-06 14:25:39 +000052};
53
Martin v. Löwis1a214512008-06-11 05:26:20 +000054
55static struct PyModuleDef cryptmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 PyModuleDef_HEAD_INIT,
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000057 "_crypt",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 NULL,
59 -1,
60 crypt_methods,
61 NULL,
62 NULL,
63 NULL,
64 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000065};
66
Mark Hammondfe51c6d2002-08-02 02:27:13 +000067PyMODINIT_FUNC
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000068PyInit__crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 return PyModule_Create(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000071}