/pypy/module/crypt/interp_crypt.py

https://bitbucket.org/pypy/pypy/ · Python · 24 lines · 22 code · 2 blank · 0 comment · 3 complexity · 2ccdb5c7293d7713abc0d68e94ac365f MD5 · raw file

  1. from pypy.interpreter.gateway import unwrap_spec
  2. from rpython.rtyper.lltypesystem import rffi
  3. from rpython.translator.tool.cbuild import ExternalCompilationInfo
  4. import sys
  5. if sys.platform.startswith('darwin'):
  6. eci = ExternalCompilationInfo()
  7. else:
  8. eci = ExternalCompilationInfo(libraries=['crypt'])
  9. c_crypt = rffi.llexternal('crypt', [rffi.CCHARP, rffi.CCHARP], rffi.CCHARP,
  10. compilation_info=eci, releasegil=False)
  11. @unwrap_spec(word=str, salt=str)
  12. def crypt(space, word, salt):
  13. """word will usually be a user's password. salt is a 2-character string
  14. which will be used to select one of 4096 variations of DES. The characters
  15. in salt must be either ".", "/", or an alphanumeric character. Returns
  16. the hashed password as a string, which will be composed of characters from
  17. the same alphabet as the salt."""
  18. res = c_crypt(word, salt)
  19. if not res:
  20. return space.w_None
  21. str_res = rffi.charp2str(res)
  22. return space.wrap(str_res)