You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.3 KiB

29 years ago
29 years ago
  1. /* cryptmodule.c - by Steve Majewski
  2. */
  3. #include "Python.h"
  4. #include <sys/types.h>
  5. /* Module crypt */
  6. /*[clinic input]
  7. module crypt
  8. [clinic start generated code]*/
  9. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
  10. /*[clinic input]
  11. crypt.crypt
  12. word: 's'
  13. salt: 's'
  14. /
  15. Hash a *word* with the given *salt* and return the hashed password.
  16. *word* will usually be a user's password. *salt* (either a random 2 or 16
  17. character string, possibly prefixed with $digit$ to indicate the method)
  18. will be used to perturb the encryption algorithm and produce distinct
  19. results for a given *word*.
  20. [clinic start generated code]*/
  21. PyDoc_STRVAR(crypt_crypt__doc__,
  22. "crypt($module, word, salt, /)\n"
  23. "--\n"
  24. "\n"
  25. "Hash a *word* with the given *salt* and return the hashed password.\n"
  26. "\n"
  27. "*word* will usually be a user\'s password. *salt* (either a random 2 or 16\n"
  28. "character string, possibly prefixed with $digit$ to indicate the method)\n"
  29. "will be used to perturb the encryption algorithm and produce distinct\n"
  30. "results for a given *word*.");
  31. #define CRYPT_CRYPT_METHODDEF \
  32. {"crypt", (PyCFunction)crypt_crypt, METH_VARARGS, crypt_crypt__doc__},
  33. static PyObject *
  34. crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt);
  35. static PyObject *
  36. crypt_crypt(PyModuleDef *module, PyObject *args)
  37. {
  38. PyObject *return_value = NULL;
  39. const char *word;
  40. const char *salt;
  41. if (!PyArg_ParseTuple(args,
  42. "ss:crypt",
  43. &word, &salt))
  44. goto exit;
  45. return_value = crypt_crypt_impl(module, word, salt);
  46. exit:
  47. return return_value;
  48. }
  49. static PyObject *
  50. crypt_crypt_impl(PyModuleDef *module, const char *word, const char *salt)
  51. /*[clinic end generated code: output=3eaacdf994a6ff23 input=4d93b6d0f41fbf58]*/
  52. {
  53. /* On some platforms (AtheOS) crypt returns NULL for an invalid
  54. salt. Return None in that case. XXX Maybe raise an exception? */
  55. return Py_BuildValue("s", crypt(word, salt));
  56. }
  57. static PyMethodDef crypt_methods[] = {
  58. CRYPT_CRYPT_METHODDEF
  59. {NULL, NULL} /* sentinel */
  60. };
  61. static struct PyModuleDef cryptmodule = {
  62. PyModuleDef_HEAD_INIT,
  63. "_crypt",
  64. NULL,
  65. -1,
  66. crypt_methods,
  67. NULL,
  68. NULL,
  69. NULL,
  70. NULL
  71. };
  72. PyMODINIT_FUNC
  73. PyInit__crypt(void)
  74. {
  75. return PyModule_Create(&cryptmodule);
  76. }