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.

71 lines
1.5 KiB

30 years ago
30 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. #include "clinic/_cryptmodule.c.h"
  11. /*[clinic input]
  12. crypt.crypt
  13. word: str
  14. salt: str
  15. /
  16. Hash a *word* with the given *salt* and return the hashed password.
  17. *word* will usually be a user's password. *salt* (either a random 2 or 16
  18. character string, possibly prefixed with $digit$ to indicate the method)
  19. will be used to perturb the encryption algorithm and produce distinct
  20. results for a given *word*.
  21. [clinic start generated code]*/
  22. static PyObject *
  23. crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
  24. /*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
  25. {
  26. char *crypt_result;
  27. #ifdef HAVE_CRYPT_R
  28. struct crypt_data data;
  29. memset(&data, 0, sizeof(data));
  30. crypt_result = crypt_r(word, salt, &data);
  31. #else
  32. crypt_result = crypt(word, salt);
  33. #endif
  34. return Py_BuildValue("s", crypt_result);
  35. }
  36. static PyMethodDef crypt_methods[] = {
  37. CRYPT_CRYPT_METHODDEF
  38. {NULL, NULL} /* sentinel */
  39. };
  40. static struct PyModuleDef cryptmodule = {
  41. PyModuleDef_HEAD_INIT,
  42. "_crypt",
  43. NULL,
  44. -1,
  45. crypt_methods,
  46. NULL,
  47. NULL,
  48. NULL,
  49. NULL
  50. };
  51. PyMODINIT_FUNC
  52. PyInit__crypt(void)
  53. {
  54. return PyModule_Create(&cryptmodule);
  55. }