(root)/
Python-3.12.0/
Modules/
_cryptmodule.c
       1  /* cryptmodule.c - by Steve Majewski
       2   */
       3  
       4  #include "Python.h"
       5  
       6  #include <sys/types.h>
       7  #ifdef HAVE_CRYPT_H
       8  #include <crypt.h>
       9  #endif
      10  
      11  /* Module crypt */
      12  
      13  /*[clinic input]
      14  module crypt
      15  [clinic start generated code]*/
      16  /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/
      17  
      18  #include "clinic/_cryptmodule.c.h"
      19  
      20  /*[clinic input]
      21  crypt.crypt
      22  
      23      word: str
      24      salt: str
      25      /
      26  
      27  Hash a *word* with the given *salt* and return the hashed password.
      28  
      29  *word* will usually be a user's password.  *salt* (either a random 2 or 16
      30  character string, possibly prefixed with $digit$ to indicate the method)
      31  will be used to perturb the encryption algorithm and produce distinct
      32  results for a given *word*.
      33  
      34  [clinic start generated code]*/
      35  
      36  static PyObject *
      37  crypt_crypt_impl(PyObject *module, const char *word, const char *salt)
      38  /*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/
      39  {
      40      char *crypt_result;
      41  #ifdef HAVE_CRYPT_R
      42      struct crypt_data data;
      43      memset(&data, 0, sizeof(data));
      44      crypt_result = crypt_r(word, salt, &data);
      45  #else
      46      crypt_result = crypt(word, salt);
      47  #endif
      48      if (crypt_result == NULL) {
      49          return PyErr_SetFromErrno(PyExc_OSError);
      50      }
      51      return Py_BuildValue("s", crypt_result);
      52  }
      53  
      54  
      55  static PyMethodDef crypt_methods[] = {
      56      CRYPT_CRYPT_METHODDEF
      57      {NULL,              NULL}           /* sentinel */
      58  };
      59  
      60  static PyModuleDef_Slot _crypt_slots[] = {
      61      {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
      62      {0, NULL}
      63  };
      64  
      65  static struct PyModuleDef cryptmodule = {
      66      PyModuleDef_HEAD_INIT,
      67      "_crypt",
      68      NULL,
      69      0,
      70      crypt_methods,
      71      _crypt_slots,
      72      NULL,
      73      NULL,
      74      NULL
      75  };
      76  
      77  PyMODINIT_FUNC
      78  PyInit__crypt(void)
      79  {
      80      return PyModuleDef_Init(&cryptmodule);
      81  }