Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5207e6

Browse files
committed
Clinic-ize the crypt module. Derby!
1 parent cc1d31e commit f5207e6

1 file changed

Lines changed: 56 additions & 15 deletions

File tree

Modules/_cryptmodule.c

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,72 @@
77

88
/* Module crypt */
99

10+
/*[clinic input]
11+
module crypt
12+
[clinic start generated code]*/
13+
/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
1014

11-
static PyObject *crypt_crypt(PyObject *self, PyObject *args)
15+
16+
/*[clinic input]
17+
crypt.crypt
18+
19+
word: 's'
20+
salt: 's'
21+
/
22+
23+
Hash a *word* with the given *salt* and return the hashed password.
24+
25+
*word* will usually be a user's password. *salt* (either a random 2 or 16
26+
character string, possibly prefixed with $digit$ to indicate the method)
27+
will be used to perturb the encryption algorithm and produce distinct
28+
results for a given *word*.
29+
30+
[clinic start generated code]*/
31+
32+
PyDoc_STRVAR(crypt_crypt__doc__,
33+
"crypt(word, salt)\n"
34+
"Hash a *word* with the given *salt* and return the hashed password.\n"
35+
"\n"
36+
"*word* will usually be a user\'s password. *salt* (either a random 2 or 16\n"
37+
"character string, possibly prefixed with $digit$ to indicate the method)\n"
38+
"will be used to perturb the encryption algorithm and produce distinct\n"
39+
"results for a given *word*.");
40+
41+
#define CRYPT_CRYPT_METHODDEF \
42+
{"crypt", (PyCFunction)crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
43+
44+
static PyObject *
45+
crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt);
46+
47+
static PyObject *
48+
crypt_crypt(PyModuleDef *module, PyObject *args)
1249
{
13-
char *word, *salt;
50+
PyObject *return_value = NULL;
51+
const char *word;
52+
const char *salt;
53+
54+
if (!PyArg_ParseTuple(args,
55+
"ss:crypt",
56+
&word, &salt))
57+
goto exit;
58+
return_value = crypt_crypt_impl(module, word, salt);
59+
60+
exit:
61+
return return_value;
62+
}
1463

15-
if (!PyArg_ParseTuple(args, "ss:crypt", &word, &salt)) {
16-
return NULL;
17-
}
64+
static PyObject *
65+
crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt)
66+
/*[clinic end generated code: checksum=a137540bf6862f9935fc112b8bb1d62d6dd1ad02]*/
67+
{
1868
/* On some platforms (AtheOS) crypt returns NULL for an invalid
1969
salt. Return None in that case. XXX Maybe raise an exception? */
2070
return Py_BuildValue("s", crypt(word, salt));
21-
2271
}
2372

24-
PyDoc_STRVAR(crypt_crypt__doc__,
25-
"crypt(word, salt) -> string\n\
26-
word will usually be a user's password. salt is a 2-character string\n\
27-
which will be used to select one of 4096 variations of DES. The characters\n\
28-
in salt must be either \".\", \"/\", or an alphanumeric character. Returns\n\
29-
the hashed password as a string, which will be composed of characters from\n\
30-
the same alphabet as the salt.");
31-
3273

3374
static PyMethodDef crypt_methods[] = {
34-
{"crypt", crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
75+
CRYPT_CRYPT_METHODDEF
3576
{NULL, NULL} /* sentinel */
3677
};
3778

0 commit comments

Comments
 (0)