_cryptmodule.c 1.4 KB
Newer Older
1 2 3
/* cryptmodule.c - by Steve Majewski
 */

Roger E. Masse's avatar
Roger E. Masse committed
4
#include "Python.h"
5 6 7 8 9

#include <sys/types.h>

/* Module crypt */

10 11 12
/*[clinic input]
module crypt
[clinic start generated code]*/
13
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
14

15
#include "clinic/_cryptmodule.c.h"
16 17 18 19

/*[clinic input]
crypt.crypt

20 21
    word: str
    salt: str
22 23 24 25 26 27 28 29 30 31 32 33
    /

Hash a *word* with the given *salt* and return the hashed password.

*word* will usually be a user's password.  *salt* (either a random 2 or 16
character string, possibly prefixed with $digit$ to indicate the method)
will be used to perturb the encryption algorithm and produce distinct
results for a given *word*.

[clinic start generated code]*/

static PyObject *
34 35
crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
/*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
36
{
37 38 39
    /* On some platforms (AtheOS) crypt returns NULL for an invalid
       salt. Return None in that case. XXX Maybe raise an exception?  */
    return Py_BuildValue("s", crypt(word, salt));
40 41
}

42

Roger E. Masse's avatar
Roger E. Masse committed
43
static PyMethodDef crypt_methods[] = {
44
    CRYPT_CRYPT_METHODDEF
45
    {NULL,              NULL}           /* sentinel */
46 47
};

48 49

static struct PyModuleDef cryptmodule = {
50
    PyModuleDef_HEAD_INIT,
51
    "_crypt",
52 53 54 55 56 57 58
    NULL,
    -1,
    crypt_methods,
    NULL,
    NULL,
    NULL,
    NULL
59 60
};

61
PyMODINIT_FUNC
62
PyInit__crypt(void)
63
{
64
    return PyModule_Create(&cryptmodule);
65
}