blob: 8e4737c7b449e969266899d9757bb86f7526ba3c [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 /* On some platforms (AtheOS) crypt returns NULL for an invalid
38 salt. Return None in that case. XXX Maybe raise an exception? */
39 return Py_BuildValue("s", crypt(word, salt));
Guido van Rossume4c61311994-05-06 14:25:39 +000040}
41
Fred Drakea664dbb2000-02-01 20:12:39 +000042
Roger E. Masse56c345b1996-12-09 23:14:26 +000043static PyMethodDef crypt_methods[] = {
Antoine Pitrouf5207e62014-01-14 21:00:27 +010044 CRYPT_CRYPT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 {NULL, NULL} /* sentinel */
Guido van Rossume4c61311994-05-06 14:25:39 +000046};
47
Martin v. Löwis1a214512008-06-11 05:26:20 +000048
49static struct PyModuleDef cryptmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 PyModuleDef_HEAD_INIT,
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000051 "_crypt",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 NULL,
53 -1,
54 crypt_methods,
55 NULL,
56 NULL,
57 NULL,
58 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000059};
60
Mark Hammondfe51c6d2002-08-02 02:27:13 +000061PyMODINIT_FUNC
Sean Reifscheidere2dfefb2011-02-22 10:55:44 +000062PyInit__crypt(void)
Guido van Rossume4c61311994-05-06 14:25:39 +000063{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 return PyModule_Create(&cryptmodule);
Guido van Rossume4c61311994-05-06 14:25:39 +000065}