| 1 |
""" |
| 2 |
CFFI based implementation of the _crypt module |
| 3 |
""" |
| 4 |
|
| 5 |
import sys |
| 6 |
import cffi |
| 7 |
import _thread |
| 8 |
_lock = _thread.allocate_lock() |
| 9 |
|
| 10 |
try: from __pypy__ import builtinify |
| 11 |
except ImportError: builtinify = lambda f: f |
| 12 |
|
| 13 |
if sys.platform == 'win32': |
| 14 |
raise ImportError("The crypt module is not supported on Windows") |
| 15 |
|
| 16 |
ffi = cffi.FFI() |
| 17 |
ffi.cdef('char *crypt(char *word, char *salt);') |
| 18 |
|
| 19 |
try: |
| 20 |
lib = ffi.dlopen('crypt') |
| 21 |
except OSError: |
| 22 |
raise ModuleNotFoundError('crypt not available', name='crypt') |
| 23 |
|
| 24 |
|
| 25 |
@builtinify |
| 26 |
def crypt(word, salt): |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
with _lock: |
| 32 |
arg_is_str = isinstance(word, str) |
| 33 |
if arg_is_str: |
| 34 |
word = word.encode('utf-8') |
| 35 |
if isinstance(salt, str): |
| 36 |
salt = salt.encode('utf-8') |
| 37 |
res = lib.crypt(word, salt) |
| 38 |
if not res: |
| 39 |
return None |
| 40 |
res = ffi.string(res) |
| 41 |
if arg_is_str: |
| 42 |
res = res.decode('utf-8') |
| 43 |
return res |