blob: a95f55a63c306e3ab724a35d80294f674226dc16 [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
Antonio Gutierrez0d3fe8a2019-10-08 06:22:17 +020045 if (crypt_result == NULL) {
46 return PyErr_SetFromErrno(PyExc_OSError);
47 }
Gregory P. Smith387512c2018-12-30 15:42:32 -080048 return Py_BuildValue("s", crypt_result);
Guido van Rossume4c61311994-05-06 14:25:39 +000049}
50
Fred Drakea664dbb2000-02-01 20:12:39 +000051
Roger E. Masse56c345b1996-12-09 23:14:26 +000052static PyMethodDef crypt_methods[] = {
Antoine Pitrouf5207e62014-01-14 21:00:27 +010053 CRYPT_CRYPT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 {NULL, NULL} /* sentinel */
Guido van Rossume4c61311994-05-06 14:25:39 +000055};
56
Hai Shib2b6e272020-02-17 17:11:34 +080057static PyModuleDef_Slot _crypt_slots[] = {
58 {0, NULL}
59};
Martin v. Löwis1a214512008-06-11 05:26:20 +000060
61static struct PyModuleDef cryptmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyModuleDef_HEAD_INIT,
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000063 "_crypt",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 NULL,
Hai Shib2b6e272020-02-17 17:11:34 +080065 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 crypt_methods,
Hai Shib2b6e272020-02-17 17:11:34 +080067 _crypt_slots,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 NULL,
69 NULL,
70 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000071};
72
Mark Hammondfe51c6d2002-08-02 02:27:13 +000073PyMODINIT_FUNC
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000074PyInit__crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000075{
Hai Shib2b6e272020-02-17 17:11:34 +080076 return PyModuleDef_Init(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000077}