_cryptmodule.c 2.31 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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

/*[clinic input]
crypt.crypt

    word: 's'
    salt: 's'
    /

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]*/

PyDoc_STRVAR(crypt_crypt__doc__,
33 34 35
"crypt($module, word, salt, /)\n"
"--\n"
"\n"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
"Hash a *word* with the given *salt* and return the hashed password.\n"
"\n"
"*word* will usually be a user\'s password.  *salt* (either a random 2 or 16\n"
"character string, possibly prefixed with $digit$ to indicate the method)\n"
"will be used to perturb the encryption algorithm and produce distinct\n"
"results for a given *word*.");

#define CRYPT_CRYPT_METHODDEF    \
    {"crypt", (PyCFunction)crypt_crypt, METH_VARARGS, crypt_crypt__doc__},

static PyObject *
crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt);

static PyObject *
crypt_crypt(PyModuleDef *module, PyObject *args)
51
{
52 53 54 55 56 57 58 59 60 61 62 63 64
    PyObject *return_value = NULL;
    const char *word;
    const char *salt;

    if (!PyArg_ParseTuple(args,
        "ss:crypt",
        &word, &salt))
        goto exit;
    return_value = crypt_crypt_impl(module, word, salt);

exit:
    return return_value;
}
65

66 67
static PyObject *
crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt)
68
/*[clinic end generated code: output=3eaacdf994a6ff23 input=4d93b6d0f41fbf58]*/
69
{
70 71 72
    /* 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));
73 74
}

75

Roger E. Masse's avatar
Roger E. Masse committed
76
static PyMethodDef crypt_methods[] = {
77
    CRYPT_CRYPT_METHODDEF
78
    {NULL,              NULL}           /* sentinel */
79 80
};

81 82

static struct PyModuleDef cryptmodule = {
83
    PyModuleDef_HEAD_INIT,
84
    "_crypt",
85 86 87 88 89 90 91
    NULL,
    -1,
    crypt_methods,
    NULL,
    NULL,
    NULL,
    NULL
92 93
};

94
PyMODINIT_FUNC
95
PyInit__crypt(void)
96
{
97
    return PyModule_Create(&cryptmodule);
98
}