blob: 72a4f44600d92c9c4e58e463196be166b0528c95 [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>
Miss Islington (bot)80285ec2021-09-29 16:02:11 -07007#ifdef HAVE_CRYPT_H
8#include <crypt.h>
9#endif
Guido van Rossume4c61311994-05-06 14:25:39 +000010
Guido van Rossume4c61311994-05-06 14:25:39 +000011/* Module crypt */
12
Antoine Pitrouf5207e62014-01-14 21:00:27 +010013/*[clinic input]
14module crypt
15[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080016/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
Guido van Rossume4c61311994-05-06 14:25:39 +000017
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030018#include "clinic/_cryptmodule.c.h"
Antoine Pitrouf5207e62014-01-14 21:00:27 +010019
20/*[clinic input]
21crypt.crypt
22
Serhiy Storchaka7e810a62015-05-30 11:09:35 +030023 word: str
24 salt: str
Antoine Pitrouf5207e62014-01-14 21:00:27 +010025 /
26
27Hash a *word* with the given *salt* and return the hashed password.
28
29*word* will usually be a user's password. *salt* (either a random 2 or 16
30character string, possibly prefixed with $digit$ to indicate the method)
31will be used to perturb the encryption algorithm and produce distinct
32results for a given *word*.
33
34[clinic start generated code]*/
35
Antoine Pitrouf5207e62014-01-14 21:00:27 +010036static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030037crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
38/*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
Antoine Pitrouf5207e62014-01-14 21:00:27 +010039{
Gregory P. Smith387512c2018-12-30 15:42:32 -080040 char *crypt_result;
41#ifdef HAVE_CRYPT_R
42 struct crypt_data data;
43 memset(&data, 0, sizeof(data));
44 crypt_result = crypt_r(word, salt, &data);
45#else
46 crypt_result = crypt(word, salt);
47#endif
Antonio Gutierrez0d3fe8a2019-10-08 06:22:17 +020048 if (crypt_result == NULL) {
49 return PyErr_SetFromErrno(PyExc_OSError);
50 }
Gregory P. Smith387512c2018-12-30 15:42:32 -080051 return Py_BuildValue("s", crypt_result);
Guido van Rossume4c61311994-05-06 14:25:39 +000052}
53
Fred Drakea664dbb2000-02-01 20:12:39 +000054
Roger E. Masse56c345b1996-12-09 23:14:26 +000055static PyMethodDef crypt_methods[] = {
Antoine Pitrouf5207e62014-01-14 21:00:27 +010056 CRYPT_CRYPT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 {NULL, NULL} /* sentinel */
Guido van Rossume4c61311994-05-06 14:25:39 +000058};
59
Hai Shib2b6e272020-02-17 17:11:34 +080060static PyModuleDef_Slot _crypt_slots[] = {
61 {0, NULL}
62};
Martin v. Löwis1a214512008-06-11 05:26:20 +000063
64static struct PyModuleDef cryptmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 PyModuleDef_HEAD_INIT,
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000066 "_crypt",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 NULL,
Hai Shib2b6e272020-02-17 17:11:34 +080068 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 crypt_methods,
Hai Shib2b6e272020-02-17 17:11:34 +080070 _crypt_slots,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 NULL,
72 NULL,
73 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000074};
75
Mark Hammondfe51c6d2002-08-02 02:27:13 +000076PyMODINIT_FUNC
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000077PyInit__crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000078{
Hai Shib2b6e272020-02-17 17:11:34 +080079 return PyModuleDef_Init(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000080}