Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(crypt_crypt__doc__, |
| 6 | "crypt($module, word, salt, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Hash a *word* with the given *salt* and return the hashed password.\n" |
| 10 | "\n" |
| 11 | "*word* will usually be a user\'s password. *salt* (either a random 2 or 16\n" |
| 12 | "character string, possibly prefixed with $digit$ to indicate the method)\n" |
| 13 | "will be used to perturb the encryption algorithm and produce distinct\n" |
| 14 | "results for a given *word*."); |
| 15 | |
| 16 | #define CRYPT_CRYPT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 17 | {"crypt", (PyCFunction)(void(*)(void))crypt_crypt, METH_FASTCALL, crypt_crypt__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 18 | |
| 19 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 20 | crypt_crypt_impl(PyObject *module, const char *word, const char *salt); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 21 | |
| 22 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 23 | crypt_crypt(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 24 | { |
| 25 | PyObject *return_value = NULL; |
| 26 | const char *word; |
| 27 | const char *salt; |
| 28 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 29 | if (!_PyArg_CheckPositional("crypt", nargs, 2, 2)) { |
| 30 | goto exit; |
| 31 | } |
| 32 | if (!PyUnicode_Check(args[0])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 33 | _PyArg_BadArgument("crypt", "argument 1", "str", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 34 | goto exit; |
| 35 | } |
| 36 | Py_ssize_t word_length; |
| 37 | word = PyUnicode_AsUTF8AndSize(args[0], &word_length); |
| 38 | if (word == NULL) { |
| 39 | goto exit; |
| 40 | } |
| 41 | if (strlen(word) != (size_t)word_length) { |
| 42 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 43 | goto exit; |
| 44 | } |
| 45 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 46 | _PyArg_BadArgument("crypt", "argument 2", "str", args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 47 | goto exit; |
| 48 | } |
| 49 | Py_ssize_t salt_length; |
| 50 | salt = PyUnicode_AsUTF8AndSize(args[1], &salt_length); |
| 51 | if (salt == NULL) { |
| 52 | goto exit; |
| 53 | } |
| 54 | if (strlen(salt) != (size_t)salt_length) { |
| 55 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 56 | goto exit; |
| 57 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 58 | return_value = crypt_crypt_impl(module, word, salt); |
| 59 | |
| 60 | exit: |
| 61 | return return_value; |
| 62 | } |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame^] | 63 | /*[clinic end generated code: output=549de0d43b030126 input=a9049054013a1b77]*/ |